home *** CD-ROM | disk | FTP | other *** search
/ WINMX Assorted Textfiles / Ebooks.tar / Text - Tech - Programming - Assembly - Assembly Tutorial (TXT).zip / ASSEMB.TXT
Text File  |  1996-06-21  |  120KB  |  4,106 lines

  1.                University of Guadalajara 
  2.           Information Sistems General Coordination.
  3.               Culture and Entertainment Web
  4.                  
  5.  June 12th 1995
  6. Copyright(C)1995-1996
  7.  
  8. This  is an introduction for people who want to programming in assembler language.
  9.  
  10. Copyright (C) 1995-1996, Hugo Perez Perez.   Anyone  may  reproduce  this document,  in  whole  or  in  part,  provided  that:   (1) any copy or republication of the entire document must show University od Guadalajara  as
  11. the  source,  and  must  include this notice; and (2) any other use of this material must reference this manual and University of Guadalajara,  and the fact that the material is copyright by Hugo Perez and is used by permission.                
  12.  
  13. Assembler Tutorial
  14. 1996 Edition
  15.  
  16. Table of Contents
  17.  
  18. 1. Introduction
  19. 2. Basic Concepts
  20. 3. Assembler programming
  21. 4. Assembler language instructions
  22. 5. Interruptions and file managing
  23. 6. Macros and procedures
  24. 7. Program examples
  25.  
  26.  
  27. 1. Introduction
  28.  
  29. Table of contents
  30.  
  31. 1.1 What's new in the Assembler material
  32. 1.2 Presentation
  33. 1.3 Why learn Assembler language
  34. 1.4 We need your opinion
  35.  
  36.                   Assembler Tutorial
  37.                          1996 Edition
  38.  
  39.                 University of Guadalajara 
  40.           Information Sistems General Coordination.
  41.               Culture and Entertainment Web 
  42.  
  43. 1.1 What's new in the Assembler material
  44.  
  45. After of one year that we've released the first Assembler material on-line.
  46. We've received a lot of e-mail where each people talk about different
  47. aspects about this material. We've tried to put these comments and
  48. suggestions in this update assembler material. We hope that this new Assembler material release reach to all people that they interest to learn the most important  language for IBM PC.
  49.  
  50. In this new assembler release includes:
  51.  
  52. A complete chapter about how to use debug program
  53. More example  of the assembler material
  54. Each section of this assembler material includes a link file to Free
  55. On-line of Computing by Dennis Howe
  56. Finally, a search engine to look for any topic or item related with this updated material.
  57.  
  58.  
  59. 1.2 Presentation
  60.  
  61. The document you are looking at, has the primordial function of introducing
  62. you to assembly language programming, and it has been thought for those
  63. people who have never worked with this language.
  64.  
  65. The tutorial is completely focused towards the computers that function with
  66. processors of the x86 family of Intel, and considering that the language
  67. bases its functioning on the internal resources of the processor, the
  68. described examples are not compatible with any other architecture.
  69.  
  70. The information was structured in units in order to allow easy access to
  71. each of the topics and facilitate the following of the tutorial.
  72.  
  73. In the introductory section some of the elemental concepts regarding
  74. computer systems are mentioned, along with the concepts of the assembly
  75. language itself, and continues with the tutorial itself.
  76.  
  77.                 Assembler Tutorial
  78.                          1996 Edition 
  79.  
  80.                 University of Guadalajara 
  81.           Information Sistems General Coordination.
  82.               Culture and Entertainment Web
  83.  
  84. 1.3 Why learn assembler language
  85.  
  86. The first reason to work with assembler is that it provides the opportunity
  87. of knowing more the operation of your PC, which allows the development of
  88. software in a more consistent manner.
  89.  
  90. The second reason is the total control of the PC which you can have with
  91. the use of the assembler.
  92.  
  93. Another reason is that the assembly programs are quicker, smaller, and have
  94. larger capacities than ones created with other languages.
  95.  
  96. Lastly, the assembler allows an ideal optimization in programs, be it on
  97. their size or on their execution.
  98.  
  99.  
  100. 1.4 We need your opinion
  101.  
  102. Our goal is offers you easier way to learn yourself assembler language. You send us your comments or suggestions about this 96' edition. Any comment will be welcome.
  103.  
  104.  
  105. 2. Basic Concepts
  106.  
  107. Table of Contents
  108.  
  109. 2.1 Basic description of a computer system.
  110. 2.2 Assembler language Basic concepts
  111. 2.3 Using debug program
  112.  
  113. 2.1 Basic description of a computer system.                                 
  114.                                
  115. This section has the purpose of giving a brief outline of the main
  116. components of a computer system at a basic level, which will allow the user
  117. a greater understanding of the concepts which will be dealt with throughout
  118. the tutorial.
  119.                   Assembler Tutorial
  120.                          1996 Edition 
  121.  
  122.                   University of Guadalajara 
  123.           Information Sistems General Coordination.
  124.               Culture and Entertainment Web 
  125.  
  126. Table of Contents
  127.  
  128. 2.1.1 Central Processor
  129. 2.1.2 Central Memory
  130.  2.1.3 Input and Output Units
  131.  2.1.4 Auxiliary Memory Units
  132.  
  133.  Computer System.
  134.  
  135. We call computer system to the complete configuration of a computer,
  136. including the peripheral units and the system programming which make it a
  137. useful and functional machine for a determined task.
  138.  
  139.  2.1.1 Central Processor.
  140.  
  141. This part is also known as central processing unit or CPU, which in turn is
  142. made  by the control unit and the arithmetic and logic unit. Its
  143. functions consist in reading and writing the contents of the memory cells,
  144. to forward data between memory cells and special registers, and decode and
  145. execute the instructions of a program. The processor has a series of memory
  146. cells which are used very often and thus, are part of the CPU. These cells
  147. are known with the name of registers. A processor may have one or two
  148. dozen of these registers. The arithmetic and logic unit of the CPU
  149. realizes the operations related with numeric and symbolic calculations.
  150. Typically these units only have capacity of performing very elemental
  151. operations such as: the addition and subtraction of two whole numbers,
  152. whole number multiplication and division, handling of the registers' bits
  153. and the comparison of the content of two registers. Personal computers can
  154. be classified by what is known as word size, this is, the quantity of bits
  155. which the processor can handle at a time.
  156.  
  157.  
  158.  2.1.2 Central Memory.
  159.  
  160. It is a group of cells, now being fabricated with semi-conductors, used for
  161. general processes, such as the execution of programs and the storage of
  162. information for the operations.
  163.  
  164.                 Assembler Tutorial
  165.                          1996 Edition 
  166.                University of Guadalajara 
  167.           Information Sistems General Coordination.
  168.               Culture and Entertainment Web 
  169.  
  170. Each one of these cells may contain a numeric value and they have the
  171. property of being addressable, this is, that they can distinguish one
  172. from another by means of a unique number or an address for each cell.
  173.  
  174. The generic name of these memories is Random Access Memory or RAM. The main disadvantage of this type of memory is that the integrated circuits lose
  175. the information they have stored when the electricity flow is interrupted.
  176. This was the reason for the creation of memories whose information is not
  177. lost when the system is turned off. These memories receive the name of Read
  178. Only Memory or ROM.
  179.  
  180.  2.1.3 Input and Output Units.
  181.  
  182. In order for a computer to be useful to us it is necessary that the
  183. processor communicates with the exterior through interfaces which allow the
  184. input and output of information from the processor and the memory. Through
  185. the use of these communications it is possible to introduce information to
  186. be processed and to later visualize the processed data.
  187.  
  188. Some of the most common input units are keyboards and mice. The most
  189. common output units are screens and printers.
  190.  
  191.  2.1.4 Auxiliary Memory Units.
  192.  
  193. Since the central memory of a computer is costly, and considering today's
  194. applications it is also very limited. Thus, the need to create practical and
  195. economical information storage systems arises. Besides, the central memory
  196. loses its content when the machine is turned off, therefore making it
  197. inconvenient for the permanent storage of data.
  198.  
  199. These and other inconvenience give place for the creation of peripheral
  200. units of memory which receive the name of auxiliary or secondary memory. Of
  201. these the most common are the tapes and magnetic discs.
  202.  
  203. The stored information on these magnetic media means receive the name of files. A file is made  of a variable number of registers, generally of a fixed
  204. size; the registers may contain information or programs.
  205.  
  206.                 Assembler Tutorial
  207.                          1996 Edition 
  208.                    University of Guadalajara 
  209.              Information Sistems General Coordination.
  210.               Culture and Entertainment Web 
  211.  
  212. 2.2 Assembler language Basic concepts 
  213.                              
  214.                                
  215. Table of Contents
  216.  
  217. 2.2.1 Information in the computers
  218.  2.2.2 Data representation methods
  219.  
  220.  
  221. 2.2.1 Information in the computer
  222.  
  223.  2.2.1.1 Information units
  224.  2.2.1.2 Numeric systems
  225.  2.2.1.3 Converting binary numbers to decimal
  226.  2.2.1.4 Converting decimal numbers to binary
  227.  2.2.1.5 Hexadecimal system
  228.  
  229.  2.2.1.1 Information Units
  230.  
  231. In order for the PC to process information, it is necessary that this
  232. information be in special cells called registers. The registers are groups of 8 or 16 flip-flops.
  233.  
  234. A flip-flop is a device capable of storing two levels of voltage, a low
  235. one, regularly 0.5 volts, and another one, commonly of 5 volts. The low
  236. level of energy in the flip-flop is interpreted as off or 0, and the high
  237. level as on or 1. These states are usually known as bits, which are the
  238. smallest information unit in a computer.
  239.  
  240. A group of 16 bits is known as word; a word can be divided in groups of 8
  241. bits called bytes, and the groups of 4 bits are called nibbles.
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.                 Assembler Tutorial
  249.                          1996 Edition 
  250.  
  251.  
  252.                    University of Guadalajara 
  253.              Information Sistems General Coordination.
  254.               Culture and Entertainment Web 
  255.  
  256.                         
  257.  
  258. 2.2.1.2 Numeric systems
  259.  
  260. The numeric system we use daily is the decimal system, but this system is
  261. not convenient for machines since the information is handled codified in
  262. the shape of on or off bits; this way of codifying takes us to the necessity
  263. of knowing the positional calculation which will allow us to express a
  264. number in any base where we need it.
  265.  
  266. It is possible to represent a determined number in any base through the
  267. following formula:
  268.  
  269.                      
  270.  
  271.  
  272. Where n is the position of the digit beginning from right to left and
  273. numbering from zero. D is the digit on which we operate and B is the used
  274. numeric base.
  275.  
  276.  
  277. 2.2.1.3 converting binary numbers to decimals
  278.  
  279. When working with assembly language we come on the necessity of converting
  280. numbers from the binary system, which is used by computers, to the decimal
  281. system used by people.
  282.  
  283. The binary system is based on only two conditions or states, be it on(1) or
  284. off(0), thus its base is two.
  285.  
  286. For the conversion we can use the positional value formula:
  287.  
  288. For example, if we have the binary number of 10011, we take each digit from
  289. right to left and multiply it by the base, elevated to the new position
  290. they are:
  291.  
  292.  
  293.                 Assembler Tutorial
  294.                          1996 Edition 
  295.                    University of Guadalajara 
  296.              Information Sistems General Coordination.
  297.               Culture and Entertainment Web 
  298.  
  299.  
  300. Binary:         1         1       0       0       1
  301.  
  302. Decimal:       1*2^0  + 1*2^1 + 0*2^2 + 0*2^3 + 1*2^4
  303.  
  304.             =   1     +   2   +   0   +   0   +  16  = 19 decimal.
  305.  
  306. The ^ character is used in computation as an exponent symbol and the *
  307. character is used to represent multiplication.
  308.  
  309.  
  310.  2.2.1.4 Converting decimal numbers to binary
  311.  
  312. There are several methods to convert decimal numbers to binary; only one
  313. will be analyzed here. Naturally a conversion with a scientific calculator
  314. is much easier, but one cannot always count with one, so it is convenient
  315. to at least know one formula to do it.
  316.  
  317. The method that will be explained uses the successive division of two,
  318. keeping the residue as a binary digit and the result as the next number to
  319. divide.
  320.  
  321. Let us take for example the decimal number of 43.
  322.  
  323. 43/2=21 and its residue is 1
  324.  
  325. 21/2=10 and its residue is 1
  326.  
  327. 10/2=5 and its residue is 0
  328.  
  329. 5/2=2 and its residue is 1
  330.  
  331. 2/2=1 and its residue is 0
  332.  
  333. 1/2=0 and its residue is 1
  334.  
  335. Building the number from the bottom , we get that the binary result is
  336. 101011
  337.                 Assembler Tutorial
  338.                          1996 Edition 
  339.                    University of Guadalajara 
  340.              Information Sistems General Coordination.
  341.               Culture and Entertainment Web 
  342.  
  343.  
  344. 2.2.1.5  Hexadecimal system
  345.  
  346. On the hexadecimal base we have 16 digits which go from 0 to 9 and from the
  347. letter A to the F, these letters represent the numbers from 10 to 15. Thus
  348. we count 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F.
  349.  
  350. The conversion between binary and hexadecimal numbers is easy. The first
  351. thing done to do a conversion of a binary number to a hexadecimal is to
  352. divide it in groups of 4 bits, beginning from the right to the left. In case
  353. the last group, the one most to the left, is under 4 bits, the missing
  354. places are filled with zeros.
  355.  
  356. Taking as an example the binary number of 101011, we divide it in 4 bits
  357. groups and we are left with:
  358.  
  359. 10;1011
  360.  
  361. Filling the last group with zeros (the one from the left):
  362.  
  363. 0010;1011
  364.  
  365. Afterwards we take each group as an independent number and we consider its
  366. decimal value:
  367.  
  368. 0010=2;1011=11
  369.  
  370. But since we cannot represent this hexadecimal number as 211 because it
  371. would be an error, we have to substitute all the values greater than 9 by
  372. their respective representation in hexadecimal, with which we obtain:
  373.  
  374. 2BH, where the H represents the hexadecimal base.
  375.  
  376. In order to convert a hexadecimal number to binary it is only necessary to
  377. invert the steps: the first hexadecimal digit is taken and converted to
  378. binary, and then the second, and so on.
  379.  
  380.  
  381.                 Assembler Tutorial
  382.                          1996 Edition 
  383.                    University of Guadalajara 
  384.              Information Sistems General Coordination.
  385.               Culture and Entertainment Web 
  386.  
  387.  
  388.  2.2.2 Data representation methods in a computer.
  389.  
  390.  2.2.2.1.ASCII code
  391.  2.2.2.2 BCD method
  392.  2.2.2.3 Floating point representation
  393.  
  394.  2.2.2.1 ASCII code
  395.  
  396. ASCII is an acronym of American Standard Code for Information Interchange.
  397. This code assigns the letters of the alphabet, decimal digits from 0 to 9
  398. and some additional symbols a binary number of 7 bits, putting the 8th bit
  399. in its off state or 0. This way each letter, digit or special character
  400. occupies one byte in the computer memory.
  401.  
  402. We can observe that this method of data representation is very inefficient
  403. on the numeric aspect, since in binary format one byte is not enough to
  404. represent numbers from 0 to 255, but on the other hand with the ASCII code
  405. one byte may represent only one digit. Due to this inefficiency, the ASCII
  406. code is mainly used in the memory to represent text.
  407.  
  408.  
  409.  2.2.2.2 BCD Method
  410.  
  411. BCD is an acronym of Binary Coded Decimal. In this notation groups of 4
  412. bits are used to represent each decimal digit from 0 to 9. With this method
  413. we can represent two digits per byte of information.
  414.  
  415. Even when this method is much more practical for number representation in
  416. the memory compared to the ASCII code, it still less practical than the
  417. binary since with the BCD method we can only represent digits from 0 to 99.
  418. On the other hand in binary format we can represent all digits from 0 to
  419. 255.
  420.  
  421. This format is mainly used to represent very large numbers in mercantile
  422. applications since it facilitates operations avoiding mistakes.
  423.  
  424.  
  425.                 Assembler Tutorial
  426.                          1996 Edition 
  427.                    University of Guadalajara 
  428.              Information Sistems General Coordination.
  429.               Culture and Entertainment Web 
  430.  
  431.  
  432.  2.2.2.3 Floating point representation
  433.  
  434. This representation is based on scientific notation, this is, to represent a
  435. number in two parts: its base and its exponent.
  436.  
  437. As an example, the number 1234000, can be represented as 1.123*10^6, in
  438. this last notation the exponent indicates to us the number of spaces that
  439. the decimal point must be moved to the right to obtain the original result.
  440.  
  441. In case the exponent was negative, it would be indicating to us the number
  442. of spaces that the decimal point must be moved to the left to obtain the
  443. original result.
  444.  
  445.   
  446. 2.3 Using Debug program                              
  447.                                
  448. Table of Contents
  449.  
  450. 2.3.1 Program creation process
  451. 2.3.2 CPU registers
  452. 2.3.3 Debug program
  453. 2.3.4 Assembler structure
  454. 2.3.5 Creating basic assembler program
  455. 2.3.6 Storing and loading the programs
  456.  2.3.7 More debug program examples
  457.  
  458. 2.31 Program creation process
  459.  
  460. For the creation of a program it is necessary to follow five steps:
  461.  
  462.       Design of the algorithm, stage the problem to be solved is
  463.      established and the best solution is proposed, creating squematic
  464.      diagrams used for the better solution proposal.
  465.      Coding the algorithm, consists in writing the program in some
  466.      programming language; assembly language in this specific case, taking
  467.      as a base the proposed solution on the prior step.
  468.  
  469.                 Assembler Tutorial
  470.                          1996 Edition 
  471.                    University of Guadalajara 
  472.              Information Sistems General Coordination.
  473.               Culture and Entertainment Web 
  474.  
  475.      Translation to machine language, is the creation of the object
  476.      program, in other words, the written program as a sequence of zeros and
  477.      ones that can be interpreted by the processor.
  478.      Test the program, after the translation the program into
  479.      machine language, execute the program in the computer machine.
  480.       The last stage is the elimination of detected faults on the
  481.      program on the test stage. The correction of a fault normally requires
  482.      the repetition of all the steps from the first or second.
  483.  
  484.  
  485. 2.3.2 CPU Registers
  486.  
  487. The CPU has 4 internal registers, each one of 16 bits. The first four, AX,
  488. BX, CX, and DX are general use registers and can also be used as 8 bit
  489. registers, if used in such a way it is necessary to refer to them for
  490. example as: AH and AL, which are the high and low bytes of the AX register.
  491. This nomenclature is also applicable to the BX, CX, and DX registers.
  492.  
  493. The registers known by their specific names:
  494.  
  495.  AX Accumulator
  496.  BX Base register
  497.  CX Counting register
  498.  DX Data register
  499.  DS Data segment register
  500.  ES Extra segment register
  501.  SS Battery segment register
  502.  CS Code segment register
  503.  BP Base pointers register
  504.  SI Source index register
  505.  DI Destiny index register
  506.  SP Battery pointer register
  507.  IP Next instruction pointer register
  508.  F Flag register
  509.  
  510.  
  511.  
  512.  
  513.                 Assembler Tutorial
  514.                          1996 Edition 
  515.                    University of Guadalajara 
  516.              Information Sistems General Coordination.
  517.               Culture and Entertainment Web 
  518.  
  519.  
  520. 2.3.3 Debug program
  521.  
  522. To create a program in assembler two options exist, the first one is to use
  523. the TASM or Turbo Assembler, of Borland, and the second one is to use the
  524. debugger - on this first section we will use this last one since it is
  525. found in any PC with the MS-DOS, which makes it available to any user who
  526. has access to a machine with these characteristics.
  527.  
  528. Debug can only create files with a .COM extension, and because of the
  529. characteristics of these kinds of programs they cannot be larger that 64
  530. kb, and they also must start with displacement, offset, or 0100H memory
  531. direction inside the specific segment.
  532.  
  533. Debug provides a set of commands that lets you perform a number of useful
  534. operations:
  535.  
  536. A Assemble symbolic instructions into machine code
  537. D Display the contents of an area of memory
  538. E Enter data into memory, beginning at a specific location
  539. G Run the executable program in memory
  540. N Name a program
  541. P Proceed, or execute a set of related instructions
  542. Q Quit the debug program
  543. R Display the contents of one or more registers
  544. T Trace the contents of one instruction
  545. U Unassembled machine code into symbolic code
  546. W Write a program onto disk
  547.  
  548. It is possible to visualize the values of the internal registers of the CPU
  549. using the Debug program. To begin working with Debug, type the following
  550. prompt in your computer:
  551.  
  552. C:/>Debug [Enter]
  553.  
  554.  
  555.  
  556.  
  557.                 Assembler Tutorial
  558.                          1996 Edition 
  559.                    University of Guadalajara 
  560.              Information Sistems General Coordination.
  561.               Culture and Entertainment Web 
  562.  
  563.  
  564. On the next line a dash will appear, this is the indicator of Debug, at
  565. this moment the instructions of Debug can be introduced using the following
  566. command:
  567.  
  568. -r[Enter]
  569.  
  570. AX=0000  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
  571. DS=0D62  ES=0D62  SS=0D62  CS=0D62  IP=0100   NV  EI PL NZ NA PO NC
  572. 0D62:0100 2E            CS:
  573. 0D62:0101 803ED3DF00    CMP     BYTE PTR [DFD3],00                 CS:DFD3=03
  574.  
  575. All the contents of the internal registers of the CPU are displayed; an
  576. alternative of viewing them is to use the "r" command using as a parameter
  577. the name of the register whose value wants to be seen. For example:
  578.  
  579. -rbx
  580. BX 0000
  581. :
  582.  
  583. This instruction will only display the content of the BX register and the
  584. Debug indicator changes from "-" to ":"
  585.  
  586. When the prompt is like this, it is possible to change the value of the
  587. register which was seen by typing the new value and [Enter], or the old
  588. value can be left by pressing [Enter] without typing any other value.
  589.  
  590.  
  591. 2.3.4 Assembler structure
  592.  
  593. In assembly language code lines have two parts, the first one is the name
  594. of the instruction which is to be executed, and the second one are the
  595. parameters of the command. For example:
  596. add ah bh
  597.  
  598. Here "add" is the command to be executed, in this case an addition, and
  599. "ah" as well as "bh" are the parameters.
  600.  
  601.                 Assembler Tutorial
  602.                          1996 Edition 
  603.                    University of Guadalajara 
  604.              Information Sistems General Coordination.
  605.               Culture and Entertainment Web 
  606.  
  607.  
  608. For example:
  609.  
  610. mov al, 25
  611.  
  612. In the above example, we are using the instruction mov, it means move the
  613. value 25 to al register.
  614.  
  615. The name of the instructions in this language is made  of two, three or
  616. four letters. These instructions are also called mnemonic names or
  617. operation codes, since they represent a function the processor will
  618. perform.
  619.  
  620. Sometimes instructions are used as follows:
  621.  
  622. add al,[170]
  623.  
  624. The brackets in the second parameter indicate to us that we are going to
  625. work with the content of the memory cell number 170 and not with the 170
  626. value, this is known as direct addressing.
  627.  
  628.  2.3.5 Creating basic assembler program
  629.  
  630. The first step is to initiate the Debug, this step only consists of typing
  631. debug[Enter] on the operative system prompt.
  632.  
  633. To assemble a program on the Debug, the "a" (assemble) command is used;
  634. when this command is used, the address where you want the assembling to
  635. begin can be given as a parameter, if the parameter is omitted the
  636. assembling will be initiated at the locality specified by CS:IP, usually
  637. 0100h, which is the locality where programs with .COM extension must be
  638. initiated. And it will be the place we will use since only Debug can create
  639. this specific type of programs.
  640.  
  641. Even though at this moment it is not necessary to give the "a" command a
  642. parameter, it is recommendable to do so to avoid problems once the CS:IP
  643. registers are used, therefore we type:
  644.  
  645.                 Assembler Tutorial
  646.                          1996 Edition 
  647.                    University of Guadalajara 
  648.              Information Sistems General Coordination.
  649.               Culture and Entertainment Web 
  650.  
  651. a 100[enter]
  652. mov ax,0002[enter]
  653. mov bx,0004[enter]
  654. add ax,bx[enter]
  655. nop[enter][enter]
  656.  
  657. What does the program do?, move the value 0002 to the ax register, move the
  658. value 0004 to the bx register, add the contents of the ax and bx registers,
  659. the instruction, no operation, to finish the program.
  660.  
  661. In the debug program. After to do this, appear on the screen some like the
  662. follow lines:
  663.  
  664. C:\>debug
  665. -a 100
  666. 0D62:0100 mov ax,0002
  667. 0D62:0103 mov bx,0004
  668. 0D62:0106 add ax,bx
  669. 0D62:0108 nop
  670. 0D62:0109
  671.  
  672. Type the command "t" (trace), to execute each instruction of this program,
  673. example:
  674.  
  675. -t
  676.  
  677. AX=0002  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
  678. DS=0D62  ES=0D62  SS=0D62  CS=0D62  IP=0103   NV  EI PL NZ NA PO NC
  679. 0D62:0103 BB0400        MOV     BX,0004
  680.  
  681. You see that the value 2 move to AX register. Type the command "t" (trace),
  682. again, and you see the second instruction is executed.
  683.  
  684. -t
  685.  
  686. AX=0002  BX=0004  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
  687. DS=0D62  ES=0D62  SS=0D62  CS=0D62  IP=0106   NV  EI PL NZ NA PO NC
  688. 0D62:0106 01D8          ADD     AX,BX
  689.                 Assembler Tutorial
  690.                          1996 Edition 
  691.                    University of Guadalajara 
  692.              Information Sistems General Coordination.
  693.               Culture and Entertainment Web 
  694.  
  695. Type the command "t" (trace) to see the instruction add is executed, you
  696. will see the follow lines:
  697.  
  698. -t
  699.  
  700. AX=0006  BX=0004  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
  701. DS=0D62  ES=0D62  SS=0D62  CS=0D62  IP=0108   NV  EI PL NZ NA PE NC
  702. 0D62:0108 90            NOP
  703.  
  704. The possibility that the registers contain different values exists, but AX
  705. and BX must be the same, since they are the ones we just modified.
  706.  
  707. To exit Debug use the "q" (quit) command.
  708.  
  709.  
  710.  2.3.6 Storing and loading the programs
  711.  
  712. It would not seem practical to type an entire program each time it is
  713. needed, and to avoid this it is possible to store a program on the disk,
  714. with the enormous advantage that by being already assembled it will not be
  715. necessary to run Debug again to execute it.
  716.  
  717. The steps to save a program that it is already stored on memory are:
  718.  
  719.       Obtain the length of the program subtracting the final address
  720.      from the initial address, naturally in hexadecimal system.
  721.       Give the program a name and extension.
  722.       Put the length of the program on the CX register.
  723.       Order Debug to write the program on the disk.
  724.  
  725. By using as an example the following program, we will have a clearer idea
  726. of how to take these steps:
  727.  
  728. When the program is finally assembled it would look like this:
  729.  
  730.  
  731.  
  732.                 Assembler Tutorial
  733.                          1996 Edition 
  734.  
  735.                    University of Guadalajara 
  736.              Information Sistems General Coordination.
  737.               Culture and Entertainment Web 
  738.  
  739.  
  740. 0C1B:0100 mov ax,0002
  741. 0C1B:0103 mov bx,0004
  742. 0C1B:0106 add ax,bx
  743. 0C1B:0108 int 20
  744. 0C1B:010A
  745.  
  746. To obtain the length of a program the "h" command is used, since it will
  747. show us the addition and subtraction of two numbers in hexadecimal. To
  748. obtain the length of ours, we give it as parameters the value of our
  749. program's final address (10A), and the program's initial address (100). The
  750. first result the command shows us is the addition of the parameters and the
  751. second is the subtraction.
  752.  
  753. -h 10a 100
  754. 020a 000a
  755.  
  756. The "n" command allows us to name the program.
  757.  
  758. -n test.com
  759.  
  760. The "rcx" command allows us to change the content of the CX register to the
  761. value we obtained from the size of the file with "h", in this case 000a,
  762. since the result of the subtraction of the final address from the initial
  763. address.
  764.  
  765. -rcx
  766. CX 0000
  767. :000a
  768.  
  769. Lastly, the "w" command writes our program on the disk, indicating how many
  770. bytes it wrote.
  771.  
  772. -w
  773. Writing 000A bytes
  774.  
  775. To save an already loaded file two steps are necessary:
  776.  
  777.                 Assembler Tutorial
  778.                          1996 Edition 
  779.                    University of Guadalajara 
  780.              Information Sistems General Coordination.
  781.               Culture and Entertainment Web 
  782.  
  783.  
  784.       Give the name of the file to be loaded.
  785.       Load it using the "l" (load) command.
  786.  
  787. To obtain the correct result of the following steps, it is necessary that
  788. the above program be already created.
  789.  
  790. Inside Debug we write the following:
  791.  
  792. -n test.com
  793. -l
  794. -u 100 109
  795. 0C3D:0100 B80200 MOV AX,0002
  796. 0C3D:0103 BB0400 MOV BX,0004
  797. 0C3D:0106 01D8 ADD AX,BX
  798. 0C3D:0108 CD20 INT 20
  799.  
  800. The last "u" command is used to verify that the program was loaded on
  801. memory. What it does is that it disassembles the code and shows it
  802. disassembled. The parameters indicate to Debug from where and to where to
  803. disassemble.
  804.  
  805. Debug always loads the programs on memory on the address 100H, otherwise
  806. indicated.
  807.  
  808.  
  809. 3 Assembler programming                               
  810.  
  811. Table of Contents
  812.  
  813. 3.1 Building Assembler programs
  814. 3.2 Assembly process
  815. 3.3 More assembler programs
  816. 3.4 Types of instructions
  817. 3.5 Click here to get more assembler programs
  818.  
  819.  
  820.  
  821.                 Assembler Tutorial
  822.                          1996 Edition 
  823.                    University of Guadalajara 
  824.              Information Sistems General Coordination.
  825.               Culture and Entertainment Web 
  826.  
  827. 3.1 Building Assembler programs
  828.  
  829. 3.1.1  Needed software
  830.  3.1.2 Assembler Programming
  831.  
  832.  
  833.  3.1.1 Needed software
  834.  
  835. In order to be able to create a program, several tools are needed:
  836.  
  837. First an editor to create the source program. Second a compiler, which is
  838. nothing more than a program that "translates" the source program into an
  839. object program. And third, a linker that generates the executable program
  840. from the object program.
  841.  
  842. The editor can be any text editor at hand, and as a compiler we will use
  843. the TASM macro assembler from Borland, and as a linker we will use the
  844. Tlink program.
  845.  
  846. The extension used so that TASM recognizes the source programs in assembler
  847. is .ASM; once translated the source program, the TASM creates a file with
  848. the .OBJ extension, this file contains an "intermediate format" of the
  849. program, called like this because it is not executable yet but it is not a
  850. program in source language either anymore. The linker generates, from a
  851. .OBJ or a combination of several of these files, an executable program,
  852. whose extension usually is .EXE though it can also be .COM, depending of
  853. the form it was assembled.
  854.  
  855. 3.1.2 Assembler Programming
  856.  
  857. To build assembler programs using TASM programs is a different program
  858. structure than from using debug program.
  859.  
  860. It's important to include the following assembler directives:
  861.  
  862. .MODEL SMALL
  863. Assembler directive that defines the memory model to use in the program
  864.  
  865.                 Assembler Tutorial
  866.                          1996 Edition 
  867.                    University of Guadalajara 
  868.              Information Sistems General Coordination.
  869.               Culture and Entertainment Web 
  870.  
  871. .CODE
  872. Assembler directive that defines the program instructions
  873.  
  874. .STACK
  875. Assembler directive that reserves a memory space for program instructions
  876. in the stack
  877.  
  878. END
  879. Assembler directive that finishes the assembler program
  880.  
  881. Let's program
  882.  
  883. First step
  884.  
  885. use any editor program to create the source file. Type the following lines:
  886.  
  887. first example
  888.  
  889. ; use ; to put comments in the assembler program
  890. .MODEL SMALL; memory model
  891. .STACK; memory space for program instructions in the stack
  892. .CODE; the following lines are program instructions
  893. mov ah,1h; moves the value 1h to register ah
  894. mov cx,07h;moves the value 07h to register cx
  895. int 10h;10h interruption
  896. mov ah,4ch;moves the value 4 ch to register ah
  897. int 21h;21h interruption
  898. END; finishes the program code
  899.  
  900. This assembler program changes the size of the computer cursor.
  901.  
  902. Second step
  903.  
  904. Save the file with the following name: examp1.asm
  905. Don't forget to save this in ASCII format.
  906.  
  907.  
  908.  
  909.                 Assembler Tutorial
  910.                          1996 Edition 
  911.                    University of Guadalajara 
  912.              Information Sistems General Coordination.
  913.               Culture and Entertainment Web 
  914.  
  915. Third step
  916.  
  917. Use the TASM program to build the object program.
  918.  
  919. Example:
  920.  
  921. C:\>tasm exam1.asm
  922. Turbo Assembler  Version 2.0  Copyright (c) 1988, 1990 Borland International
  923.  
  924. Assembling file:   exam1.asm
  925. Error messages:    None
  926. Warning messages:  None
  927. Passes:            1
  928. Remaining memory:  471k
  929.  
  930. The TASM can only create programs in .OBJ format, which are not executable
  931. by themselves, but rather it is necessary to have a linker which generates
  932. the executable code.
  933.  
  934. Fourth step
  935.  
  936. Use the TLINK program to build the executable program example:
  937.  
  938. C:\>tlink exam1.obj
  939. Turbo Link  Version 3.0 Copyright (c) 1987, 1990 Borland International
  940.  
  941. C:\>
  942.  
  943. Where exam1.obj is the name of the intermediate program, .OBJ. This
  944. generates a file directly with the name of the intermediate program and the
  945. .EXE extension.
  946.  
  947. Fifth step
  948.  
  949. Execute the executable program
  950.  
  951. C:\>exam1[enter]
  952.  
  953.                 Assembler Tutorial
  954.                          1996 Edition 
  955.                    University of Guadalajara 
  956.              Information Sistems General Coordination.
  957.               Culture and Entertainment Web 
  958.  
  959. Remember, this assembler program changes the size of the cursor.
  960.  
  961. Assembly process.
  962.  
  963.  Segments
  964.  Table of symbols
  965.  
  966.  SEGMENTS
  967.  
  968. The architecture of the x86 processors forces to the use of memory segments
  969. to manage the information, the size of these segments is of 64kb.
  970.  
  971. The reason of being of these segments is that, considering that the maximum
  972. size of a number that the processor can manage is given by a word of 16
  973. bits or register, it would not be possible to access more than 65536
  974. localities of memory using only one of these registers, but now, if the
  975. PC's memory is divided into groups or segments, each one of 65536
  976. localities, and we use an address on an exclusive register to find each
  977. segment, and then we make each address of a specific slot with two
  978. registers, it is possible for us to access a quantity of 4294967296 bytes
  979. of memory, which is, in the present day, more memory than what we will see
  980. installed in a PC.
  981.  
  982. In order for the assembler to be able to manage the data, it is necessary
  983. that each piece of information or instruction be found in the area that
  984. corresponds to its respective segments. The assembler accesses this
  985. information taking into account the localization of the segment, given by
  986. the DS, ES, SS and CS registers and inside the register the address of the
  987. specified piece of information. It is because of this that when we create a
  988. program using the Debug on each line that we assemble, something like this
  989. appears:
  990.  
  991. 1CB0:0102 MOV AX,BX
  992.  
  993. Where the first number, 1CB0, corresponds to the memory segment being used,
  994. the second one refers to the address inside this segment, and the
  995. instructions which will be stored from that address follow.
  996.  
  997.                 Assembler Tutorial
  998.                          1996 Edition 
  999.                    University of Guadalajara 
  1000.              Information Sistems General Coordination.
  1001.               Culture and Entertainment Web 
  1002.  
  1003. The way to indicate to the assembler with which of the segments we will
  1004. work with is with the .CODE, .DATA and .STACK directives.
  1005.  
  1006. The assembler adjusts the size of the segments taking as a base the number
  1007. of bytes each assembled instruction needs, since it would be a waste of
  1008. memory to use the whole segments. For example, if a program only needs 10kb
  1009. to store data, the data segment will only be of 10kb and not the 64kb it
  1010. can handle.
  1011.  
  1012.  SYMBOLS CHART
  1013.  
  1014. Each one of the parts on code line in assembler is known as token, for
  1015. example on the code line:
  1016.  
  1017. MOV AX,Var
  1018.  
  1019. we have three tokens, the MOV instruction, the AX operator, and the VAR
  1020. operator. What the assembler does to generate the OBJ code is to read each
  1021. one of the tokens and look for it on an internal "equivalence" chart known
  1022. as the reserved words chart, which is where all the mnemonic meanings we
  1023. use as instructions are found.
  1024.  
  1025. Following this process, the assembler reads MOV, looks for it on its chart
  1026. and identifies it as a processor instruction. Likewise it reads AX and
  1027. recognizes it as a register of the processor, but when it looks for the Var
  1028. token on the reserved words chart, it does not find it, so then it looks
  1029. for it on the symbols chart which is a table where the names of the
  1030. variables, constants and labels used in the program where their addresses
  1031. on memory are included and the sort of data it contains, are found.
  1032.  
  1033. Sometimes the assembler comes on a token which is not defined on the
  1034. program, therefore what it does in these cased is to pass a second time by
  1035. the source program to verify all references to that symbol and place it on
  1036. the symbols chart.
  1037.  
  1038.  
  1039.                 
  1040.                                 
  1041.                 Assembler Tutorial
  1042.                          1996 Edition 
  1043.                    University of Guadalajara 
  1044.              Information Sistems General Coordination.
  1045.               Culture and Entertainment Web 
  1046.  
  1047.  
  1048.  There are symbols which the assembler will not find since they do not belong to that segment and the program does not know in what part of the memory it will find that segment, and at this time the linker comes into action, which will create the structure necessary for the loader so that the segment and the token be defined when the program is loaded and before it is executed.
  1049.  
  1050. 3.3 More assembler programs
  1051.  
  1052. Another example
  1053.  
  1054. first step
  1055.  
  1056. use any editor program to create the source file. Type the following lines:
  1057.  
  1058. ;example11
  1059. .model small
  1060. .stack
  1061. .code
  1062. mov ah,2h  ;moves the value 2h to register ah
  1063. mov dl,2ah ;moves de value 2ah to register dl
  1064.    ;(Its the asterisk value in ASCII format)
  1065. int 21h    ;21h interruption
  1066. mov ah,4ch ;4ch function, goes to operating system
  1067. int 21h    ;21h interruption
  1068. end        ;finishes the program code
  1069.  
  1070. second step
  1071.  
  1072. Save the file with the following name: exam2.asm
  1073. Don't forget to save this in ASCII format.
  1074.  
  1075. third step
  1076.  
  1077. Use the TASM program to build the object program.
  1078.  
  1079. C:\>tasm exam2.asm
  1080. Turbo Assembler  Version 2.0  Copyright (c) 1988, 1990 Borland International
  1081.                 Assembler Tutorial
  1082.                          1996 Edition 
  1083.                    University of Guadalajara 
  1084.              Information Sistems General Coordination.
  1085.               Culture and Entertainment Web 
  1086.  
  1087. Assembling file:   exam2.asm
  1088. Error messages:    None
  1089. Warning messages:  None
  1090. Passes:            1
  1091. Remaining memory:  471k
  1092.  
  1093. fourth step
  1094.  
  1095. Use the TLINK program to build the executable program
  1096.  
  1097. C:\>tlink exam2.obj
  1098. Turbo Link  Version 3.0 Copyright (c) 1987, 1990 Borland International
  1099.  
  1100. C:\>
  1101.  
  1102. fifth step
  1103.  
  1104. Execute the executable program
  1105.  
  1106. C:\>ejem11[enter]
  1107. *
  1108. C:\>
  1109.  
  1110. This assembler program shows the asterisk character on the computer screen
  1111.  
  1112.  
  1113. 3.4 Types of instructions.
  1114.  
  1115. 3.4.1 Data movement
  1116.  3.4.2 Logic and arithmetic operations
  1117.  3.4.3 Jumps, loops and procedures
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.                 Assembler Tutorial
  1126.                          1996 Edition 
  1127.                    University of Guadalajara 
  1128.              Information Sistems General Coordination.
  1129.               Culture and Entertainment Web 
  1130.  
  1131.  3.4.1 Data  movement
  1132.  
  1133. In any program it is necessary to move the data in the memory and in the CPU
  1134. registers; there are several ways to do this: it can copy data in the
  1135. memory to some register, from register to register, from a register to a
  1136. stack, from a stack to a register, to transmit data to external devices as
  1137. well as vice versa.
  1138.  
  1139. This movement of data is subject to rules and restrictions. The following
  1140. are some of them:
  1141.  
  1142. *It is not possible to move data from a memory locality to another
  1143. directly; it is necessary to first move the data of the origin locality to a
  1144. register and then from the register to the destiny locality.
  1145.  
  1146. *It is not possible to move a constant directly to a segment register; it
  1147. first must be moved to a register in the CPU.
  1148.  
  1149. It is possible to move data blocks by means of the movs instructions, which
  1150. copies a chain of bytes or words; movsb which copies n bytes from a
  1151. locality to another; and movsw copies n words from a locality to another.
  1152. The last two instructions take the values from the defined addresses by
  1153. DS:SI as a group of data to move and ES:DI as the new localization of the
  1154. data.
  1155.  
  1156. To move data there are also structures called batteries, where the data is
  1157. introduced with the push instruction and are extracted with the pop
  1158. instruction.
  1159. In a stack the first data to be introduced is the last one we can take,
  1160. this is, if in our program we use these instructions:
  1161.  
  1162.   PUSH AX
  1163.   PUSH BX
  1164.   PUSH CX
  1165.  
  1166. To return the correct values to each register at the moment of taking them
  1167. from the stack it is necessary to do it in the following order:
  1168.  
  1169.                 Assembler Tutorial
  1170.                          1996 Edition 
  1171.                    University of Guadalajara 
  1172.              Information Sistems General Coordination.
  1173.               Culture and Entertainment Web 
  1174.  
  1175.   POP CX
  1176.   POP BX
  1177.   POP AX
  1178.  
  1179. For the communication with external devices the out command is used to send
  1180. information to a port and the in command to read the information received
  1181. from a port.
  1182.  
  1183. The syntax of the out command is:
  1184.  
  1185. OUT DX,AX
  1186.  
  1187. Where DX contains the value of the port which will be used for the
  1188. communication and AX contains the information which will be sent.
  1189.  
  1190. The syntax of the in command is:
  1191.  
  1192. IN AX,DX
  1193.  
  1194. Where AX is the register where the incoming information will be kept and DX
  1195. contains the address of the port by which the information will arrive.
  1196.  
  1197. 3.4.2 Logic and arithmetic operations
  1198.  
  1199. The instructions of the logic operations are: and, not, or and xor. These
  1200. work on the bits of their operators.
  1201. To verify the result of the operations we turn to the cmp and test
  1202. instructions.
  1203. The instructions used for the algebraic operations are: to  add, to
  1204. subtract sub, to multiply mul and to divide div.
  1205.  
  1206. Almost all the comparison instructions are based on the information
  1207. contained in the flag register. Normally the flags of this register which
  1208. can be directly handled by the programmer are the data direction flag DF,
  1209. used to define the operations about chains. 
  1210.  
  1211.  
  1212.  
  1213.                 Assembler Tutorial
  1214.                          1996 Edition 
  1215.                    University of Guadalajara 
  1216.              Information Sistems General Coordination.
  1217.               Culture and Entertainment Web 
  1218.  
  1219. Another one which can also be handled is the IF flag by means of the sti and cli instructions, to activate and deactivate the interruptions.
  1220.  
  1221. 3.4.3  Jumps, loops and procedures
  1222.  
  1223. The unconditional jumps in a written program in assembler language are given
  1224. by the jmp instruction; a jump is to moves the flow of the execution of
  1225. a program by sending the control to the indicated address.
  1226.  
  1227. A loop, known also as iteration, is the repetition of a process a certain
  1228. number of times until a condition is fulfilled. These loops are used
  1229.   
  1230. 4 Assembler language Instructions
  1231.  
  1232.                            
  1233. Table of Contents
  1234.  
  1235.  4.1 Transfer instructions
  1236.  4.2 Loading instructions
  1237.  4.3 Stack instructions
  1238. 4.4 Logic instructions
  1239. 4.5 Arithmetic instructions
  1240. 4.6 Jump instructions
  1241. 4.7 Instructions for cycles: loop
  1242. 4.8 Counting Instructions
  1243. 4.9 Comparison Instructions
  1244. 4.10 Flag Instructions
  1245.  
  1246.  
  1247.  4.1 Transfer instructions
  1248.  
  1249. They are used to move the contents of the operators. Each instruction can
  1250. be used with different modes of addressing.
  1251.  
  1252.  MOV
  1253.  MOVS (MOVSB) (MOVSW)
  1254.  
  1255.                 
  1256.                 Assembler Tutorial
  1257.                          1996 Edition 
  1258.                    University of Guadalajara 
  1259.              Information Sistems General Coordination.
  1260.               Culture and Entertainment Web 
  1261.  
  1262.  
  1263.  MOV INSTRUCTION
  1264.  
  1265. Purpose: Data transfer between memory cells, registers and the accumulator.
  1266.  
  1267. Syntax:
  1268.  
  1269. MOV Destiny, Source
  1270.  
  1271. Where Destiny is the place where the data will be moved and Source is the
  1272. place where the data is.
  1273.  
  1274. The different movements of data allowed for this instruction are:
  1275.  
  1276. *Destiny:  memory.  Source:  accumulator
  1277. *Destiny:  accumulator.  Source:  memory
  1278. *Destiny:  segment register.  Source:  memory/register
  1279. *Destiny:  memory/register.  Source:  segment register
  1280. *Destiny:  register.  Source:  register
  1281. *Destiny:  register.  Source:  memory
  1282. *Destiny:  memory.  Source:  register
  1283. *Destiny:  register.  Source:  immediate data
  1284. *Destiny:  memory.  Source:  immediate data
  1285.  
  1286. Example:
  1287.  
  1288.   MOV AX,0006h
  1289.   MOV BX,AX
  1290.   MOV AX,4C00h
  1291.   INT 21H
  1292.  
  1293. This small program moves the value of 0006H to the AX register, then it
  1294. moves the content of AX (0006h) to the BX register, and lastly it moves the
  1295. 4C00h value to the AX register to end the execution with the 4C option of
  1296. the 21h interruption.
  1297.  
  1298.  
  1299.  
  1300.                 Assembler Tutorial
  1301.                          1996 Edition 
  1302.                    University of Guadalajara 
  1303.              Information Sistems General Coordination.
  1304.               Culture and Entertainment Web 
  1305.  
  1306.  MOVS (MOVSB) (MOVSW) Instruction
  1307.  
  1308. Purpose: To move byte or word chains from the source, addressed by SI, to
  1309. the destiny addressed by DI.
  1310.  
  1311. Syntax:
  1312.  
  1313. MOVS
  1314.  
  1315. This command does not need parameters since it takes as source address the
  1316. content of the SI register and as destination the content of DI. The
  1317. following sequence of instructions illustrates this:
  1318.  
  1319. MOV SI, OFFSET VAR1
  1320. MOV DI, OFFSET VAR2
  1321. MOVS
  1322.  
  1323. First we initialize the values of SI and DI with the addresses of the VAR1
  1324. and VAR2 variables respectively, then after executing MOVS the content of
  1325. VAR1 is copied onto VAR2.
  1326.  
  1327. The MOVSB and MOVSW are used in the same way as MOVS, the first one moves one byte and the second one moves a word.
  1328.  
  1329.  4.2 Loading instructions
  1330.  
  1331. They are specific register instructions. They are used to load bytes or
  1332. chains of bytes onto a register.
  1333.  
  1334.  LODS (LODSB) (LODSW)
  1335.  LAHF
  1336.  LDS
  1337.  LEA
  1338.  LES
  1339.  
  1340.  
  1341.  
  1342.  
  1343.                 Assembler Tutorial
  1344.                          1996 Edition 
  1345.                    University of Guadalajara 
  1346.              Information Sistems General Coordination.
  1347.               Culture and Entertainment Web 
  1348.  
  1349.  LODS (LODSB) (LODSW) INSTRUCTION
  1350.  
  1351. Purpose: To load chains of a byte or a word into the accumulator.
  1352.  
  1353. Syntax:
  1354.  
  1355. LODS
  1356.  
  1357. This instruction takes the chain found on the address specified by SI,
  1358. loads it to the AL (or AX) register and adds or subtracts , depending on
  1359. the state of DF, to SI if it is a bytes transfer or if it is a words
  1360. transfer.
  1361.  
  1362. MOV SI, OFFSET VAR1
  1363. LODS
  1364.  
  1365. The first line loads the VAR1 address on SI and the second line takes the
  1366. content of that locality to the AL register.
  1367.  
  1368. The LODSB and LODSW commands are used in the same way, the first one loads a byte and the second one a word (it uses the complete AX register).
  1369.  
  1370.  LAHF INSTRUCTION
  1371.  
  1372. Purpose: It transfers the content of the flags to the AH register.
  1373.  
  1374. Syntax:
  1375.  
  1376. LAHF
  1377.  
  1378. This instruction is useful to verify the state of the flags during the
  1379. execution of our program.
  1380.  
  1381. The flags are left in the following order inside the register:
  1382.  
  1383. SF ZF ?? AF ?? PF ?? CF
  1384.  
  1385. The "??" means that there will be an undefined value in those bits.
  1386.                 Assembler Tutorial
  1387.                          1996 Edition 
  1388.                    University of Guadalajara 
  1389.              Information Sistems General Coordination.
  1390.               Culture and Entertainment Web 
  1391.  
  1392.  LDS INSTRUCTION
  1393.  
  1394. Purpose: To load the register of the data segment
  1395.  
  1396. Syntax:
  1397.  
  1398. LDS destiny, source
  1399.  
  1400. The source operator must be a double word in memory. The word associated
  1401. with the largest address is transferred to DS, in other words it is taken as
  1402. the segment address. The word associated with the smaller address is the
  1403. displacement address and it is deposited in the register indicated as
  1404. destiny.
  1405.  
  1406.  LEA INSTRUCTION
  1407.  
  1408. Purpose: To load the address of the source operator
  1409.  
  1410. Syntax:
  1411.  
  1412. LEA destiny, source
  1413.  
  1414. The source operator must be located in memory, and its displacement is
  1415. placed on the index register or specified pointer in destiny.
  1416.  
  1417. To illustrate one of the facilities we have with this command let us write
  1418. an equivalence:
  1419.  
  1420. MOV SI,OFFSET VAR1
  1421.  
  1422. Is equivalent to:
  1423.  
  1424. LEA SI,VAR1
  1425.  
  1426. It is very probable that for the programmer it is much easier to create
  1427. extensive programs by using this last format.
  1428.  
  1429.  
  1430.                 Assembler Tutorial
  1431.                          1996 Edition 
  1432.                    University of Guadalajara 
  1433.              Information Sistems General Coordination.
  1434.               Culture and Entertainment Web 
  1435.  
  1436.  LES INSTRUCTION
  1437.  
  1438. Purpose: To load the register of the extra segment
  1439.  
  1440. Syntax:
  1441.  
  1442. LES destiny, source
  1443.  
  1444. The source operator must be a double word operator in memory. The content
  1445. of the word with the larger address is interpreted as the segment address
  1446. and it is placed in ES. The word with the smaller address is the
  1447. displacement address and it is placed in the specified register on the
  1448. destiny parameter.
  1449.  
  1450.  4.3 Stack instructions
  1451.  
  1452. These instructions allow the use of the stack to store or retrieve data.
  1453.  
  1454.  POP
  1455.  POPF
  1456.  PUSH
  1457.  PUSHF
  1458.  
  1459.  
  1460.  POP INSTRUCTION
  1461.  
  1462. Purpose: It recovers a piece of information from the stack
  1463.  
  1464. Syntax:
  1465.  
  1466. POP destiny
  1467.  
  1468. This instruction transfers the last value stored on the stack to the
  1469. destiny operator, it then increases by 2 the SP register.
  1470.  
  1471.  
  1472.  
  1473.  
  1474.                 Assembler Tutorial
  1475.                          1996 Edition 
  1476.                    University of Guadalajara 
  1477.              Information Sistems General Coordination.
  1478.               Culture and Entertainment Web 
  1479.  
  1480. This increase is due to the fact that the stack grows from the highest
  1481. memory segment address to the lowest, and the stack only works with words,
  1482. 2 bytes, so then by increasing by two the SP register, in reality two are
  1483. being subtracted from the real size of the stack.
  1484.  
  1485.  POPF INSTRUCTION
  1486.  
  1487. Purpose: It extracts the flags stored on the stack
  1488.  
  1489. Syntax:
  1490.  
  1491. POPF
  1492.  
  1493. This command transfers bits of the word stored on the higher part of the
  1494. stack to the flag register.
  1495.  
  1496. The way of transference is as follows:
  1497.  
  1498. BIT    FLAG
  1499.  
  1500.   0     CF
  1501.   2     PF
  1502.   4     AF
  1503.   6     ZF
  1504.   7     SF
  1505.   8     TF
  1506.   9     IF
  1507.  10     DF
  1508.  11     OF
  1509.  
  1510. These localities are the same for the PUSHF command.
  1511.  
  1512. Once the transference is done the SP register is increased by 2,
  1513. diminishing the size of the stack.
  1514.  
  1515.  
  1516.  
  1517.  
  1518.                 Assembler Tutorial
  1519.                          1996 Edition 
  1520.                    University of Guadalajara 
  1521.              Information Sistems General Coordination.
  1522.               Culture and Entertainment Web 
  1523.  
  1524.  PUSH INSTRUCTION
  1525.  
  1526. Purpose: It places a word on the stack.
  1527.  
  1528. Syntax:
  1529.  
  1530. PUSH source
  1531.  
  1532. The PUSH instruction decreases by two the value of SP and then transfers
  1533. the content of the source operator to the new resulting address on the
  1534. recently modified register.
  1535.  
  1536. The decrease on the address is due to the fact that when adding values to
  1537. the stack, this one grows from the greater to the smaller segment address,
  1538. therefore by subtracting 2 from the SP register what we do is to increase
  1539. the size of the stack by two bytes, which is the only quantity of
  1540. information the stack can handle on each input and output of information.
  1541.  
  1542.  PUSHF INSTRUCTION
  1543.  
  1544. Purpose: It places the value of the flags on the stack.
  1545.  
  1546. Syntax:
  1547.  
  1548. PUSHF
  1549.  
  1550. This command decreases by 2 the value of the SP register and then the
  1551. content of the flag register is transferred to the stack, on the address
  1552. indicated by SP.
  1553.  
  1554. The flags are left stored in memory on the same bits indicated on the POPF
  1555. command.
  1556.  
  1557.                         
  1558. 4.4 Logic instructions
  1559.  
  1560. They are used to perform logic operations on the operators.
  1561.  
  1562.                 Assembler Tutorial
  1563.                          1996 Edition 
  1564.                    University of Guadalajara 
  1565.              Information Sistems General Coordination.
  1566.               Culture and Entertainment Web 
  1567.  
  1568.  AND
  1569.  NEG
  1570.  NOT
  1571.  OR
  1572.  TEST
  1573.  XOR
  1574.  
  1575.  
  1576.  AND INSTRUCTION
  1577.  
  1578. Purpose: It performs the conjunction of the operators bit by bit.
  1579.  
  1580. Syntax:
  1581.  
  1582. AND destiny, source
  1583.  
  1584. With this instruction the "y" logic operation for both operators is carried
  1585. out:
  1586.  
  1587. Source   Destiny  |   Destiny
  1588. -----------------------------
  1589.   1         1     |     1
  1590.   1         0     |     0
  1591.   0         1     |     0
  1592.   0         0     |     0
  1593.  
  1594. The result of this operation is stored on the destiny operator.
  1595.  
  1596.  NEG INSTRUCTION
  1597.  
  1598. Purpose: It generates the complement to 2.
  1599.  
  1600. Syntax:
  1601.  
  1602. NEG destiny
  1603.  
  1604. This instruction generates the complement to 2 of the destiny operator and
  1605. stores it on the same operator. 
  1606.                 Assembler Tutorial
  1607.                          1996 Edition 
  1608.                    University of Guadalajara 
  1609.              Information Sistems General Coordination.
  1610.               Culture and Entertainment Web 
  1611.  
  1612.  
  1613. For example, if AX stores the value of 1234H, then:
  1614.  
  1615. NEG AX
  1616.  
  1617. This would leave the EDCCH value stored on the AX register.
  1618.  
  1619.  NOT INSTRUCTION
  1620.  
  1621. Purpose: It carries out the negation of the destiny operator bit by bit.
  1622.  
  1623. Syntax:
  1624.  
  1625. NOT destiny
  1626.  
  1627. The result is stored on the same destiny operator.
  1628.  
  1629.  OR INSTRUCTION
  1630.  
  1631. Purpose: Logic inclusive OR
  1632.  
  1633. Syntax:
  1634.  
  1635. OR destiny, source
  1636.  
  1637. The OR instruction carries out, bit by bit, the logic inclusive disjunction
  1638. of the two operators:
  1639.  
  1640. Source     Destiny    |    Destiny
  1641. -----------------------------------
  1642.    1          1       |       1
  1643.    1          0       |       1
  1644.    0          1       |       1
  1645.    0          0       |       0
  1646.  
  1647.  
  1648.  
  1649.  
  1650.                 Assembler Tutorial
  1651.                          1996 Edition 
  1652.                    University of Guadalajara 
  1653.              Information Sistems General Coordination.
  1654.               Culture and Entertainment Web 
  1655.  
  1656. TEST INSTRUCTION
  1657.  
  1658. Purpose: It logically compares the operators 
  1659.  
  1660. Syntax:
  1661.  
  1662. TEST destiny, source
  1663.  
  1664. It performs a conjunction, bit by bit, of the operators, but differing from
  1665. AND, this instruction does not place the result on the destiny operator, it
  1666. only has effect on the state of the flags.
  1667.  
  1668.  XOR INSTRUCTION
  1669.  
  1670. Purpose: OR exclusive
  1671.  
  1672. Syntax:
  1673.  
  1674. XOR destiny, source Its function is to perform the logic exclusive
  1675. disjunction of the two operators bit by bit.
  1676.  
  1677. Source     Destiny    |    Destiny
  1678. -----------------------------------
  1679.    1          1       |       0
  1680.    0          0       |       1
  1681.    0          1       |       1
  1682.    0          0       |       0
  1683.  
  1684. 4.5 Arithmetic instructions
  1685.  
  1686. They are used to perform arithmetic operations on the operators.
  1687.  
  1688.  ADC
  1689.  ADD
  1690.  DIV
  1691.  IDIV
  1692.  
  1693.  
  1694.                 Assembler Tutorial
  1695.                          1996 Edition 
  1696.                    University of Guadalajara 
  1697.              Information Sistems General Coordination.
  1698.               Culture and Entertainment Web 
  1699.  
  1700.  MUL
  1701.  IMUL
  1702.  SBB
  1703.  SUB
  1704.  
  1705.  
  1706.  ADC INSTRUCTION
  1707.  
  1708. Purpose: Cartage addition
  1709.  
  1710. Syntax:
  1711.  
  1712. ADC destiny, source
  1713.  
  1714. It carries out the addition of two operators and adds one to the result in
  1715. case the CF flag is activated, this is in case there is carried.
  1716.  
  1717. The result is stored on the destiny operator.
  1718.  
  1719.  ADD INSTRUCTION
  1720.  
  1721. Purpose: Addition of the operators.
  1722.  
  1723. Syntax:
  1724.  
  1725. ADD destiny, source
  1726.  
  1727. It adds the two operators and stores the result on the destiny operator.
  1728.  
  1729.  DIV INSTRUCTION
  1730.  
  1731. Purpose: Division without sign.
  1732.  
  1733. Syntax:
  1734.  
  1735. DIV source
  1736.  
  1737.                 
  1738.                 Assembler Tutorial
  1739.                          1996 Edition 
  1740.                    University of Guadalajara 
  1741.              Information Sistems General Coordination.
  1742.               Culture and Entertainment Web 
  1743.  
  1744. The divider can be a byte or a word and it is the operator which is given
  1745. the instruction.
  1746.  
  1747. If the divider is 8 bits, the 16 bits AX register is taken as dividend and
  1748. if the divider is 16 bits the even DX:AX register will be taken as
  1749. dividend, taking the DX high word and AX as the low.
  1750.  
  1751. If the divider was a byte then the quotient will be stored on the AL
  1752. register and the residue on AH, if it was a word then the quotient is
  1753. stored on AX and the residue on DX.
  1754.  
  1755.  IDIV INSTRUCTION
  1756.  
  1757. Purpose: Division with sign.
  1758.  
  1759. Syntax:
  1760.  
  1761. IDIV source
  1762.  
  1763. It basically consists on the same as the DIV instruction, and the only
  1764. difference is that this one performs the operation with sign.
  1765.  
  1766. For its results it used the same registers as the DIV instruction.
  1767.  
  1768.  MUL INSTRUCTION
  1769.  
  1770. Purpose: Multiplication with sign.
  1771.  
  1772. Syntax:
  1773.  
  1774. MUL source
  1775.  
  1776. The assembler assumes that the multiplicand will be of the same size as the
  1777. multiplier, therefore it multiplies the value stored on the register given
  1778. as operator by the one found to be contained in AH if the multiplier is 8
  1779. bits or by AX if the multiplier is 16 bits.
  1780.  
  1781.  
  1782.                 Assembler Tutorial
  1783.                          1996 Edition 
  1784.                    University of Guadalajara 
  1785.              Information Sistems General Coordination.
  1786.               Culture and Entertainment Web 
  1787.  
  1788. When a multiplication is done with 8 bit values, the result is stored on
  1789. the AX register and when the multiplication is with 16 bit values the
  1790. result is stored on the even DX:AX register.
  1791.  
  1792.  IMUL INSTRUCTION
  1793.  
  1794. Purpose: Multiplication of two whole numbers with sign.
  1795.  
  1796. Syntax:
  1797.  
  1798. IMUL source
  1799.  
  1800. This command does the same as the one before, only that this one does take
  1801. into account the signs of the numbers being multiplied.
  1802.  
  1803. The results are kept in the same registers that the MOV instruction uses.
  1804.  
  1805.  SBB INSTRUCTION
  1806.  
  1807. Purpose: Subtraction with cartage.
  1808.  
  1809. Syntax:
  1810.  
  1811. SBB destiny, source
  1812.  
  1813. This instruction subtracts the operators and subtracts one to the result if
  1814. CF is activated. The source operator is always subtracted from the destiny.
  1815.  
  1816. This kind of subtraction is used when one is working with 32 bits
  1817. quantities.
  1818.  
  1819.  SUB INSTRUCTION
  1820.  
  1821. Purpose: Subtraction.
  1822.  
  1823.  
  1824.  
  1825.  
  1826.                 Assembler Tutorial
  1827.                          1996 Edition 
  1828.                    University of Guadalajara 
  1829.              Information Sistems General Coordination.
  1830.               Culture and Entertainment Web 
  1831.  
  1832. Syntax:
  1833.  
  1834. SUB destiny, source
  1835.  
  1836. It subtracts the source operator from the destiny.
  1837.                     
  1838. 4.6 Jump instructions
  1839. 4.7 Instructions for cycles: loop
  1840. 4.8 Counting Instructions
  1841. 4.9 Comparison Instructions
  1842. 4.10 Flag Instructions
  1843.  
  1844.  
  1845.  4.6 Jump instructions
  1846.  
  1847. They are used to transfer the flow of the process to the indicated
  1848. operator.
  1849.  
  1850.  JMP
  1851.  JA (JNBE)
  1852.  JAE (JNBE)
  1853.  JB (JNAE)
  1854.  JBE (JNA)
  1855.  JE (JZ)
  1856.  JNE (JNZ)
  1857.  JG (JNLE)
  1858.  JGE (JNL)
  1859.  JL (JNGE)
  1860.  JLE (JNG)
  1861.  JC
  1862.  JNC
  1863.  JNO
  1864.  JNP (JPO)
  1865.  JNS
  1866.  JO
  1867.  JP (JPE)
  1868.  JS
  1869.  
  1870.                 Assembler Tutorial
  1871.                          1996 Edition 
  1872.                    University of Guadalajara 
  1873.              Information Sistems General Coordination.
  1874.               Culture and Entertainment Web 
  1875.  
  1876.  
  1877.  JMP INSTRUCTION
  1878.  
  1879. Purpose: Unconditional jump.
  1880.  
  1881. Syntax:
  1882.  
  1883. JMP destiny
  1884.  
  1885. This instruction is used to deviate the flow of a program without taking
  1886. into account the actual conditions of the flags or of the data.
  1887.  
  1888.  JA (JNBE) INSTRUCTION
  1889.  
  1890. Purpose: Conditional jump.
  1891.  
  1892. Syntax:
  1893.  
  1894. JA Label
  1895.  
  1896. After a comparison this command jumps if it is  or jumps if it is not
  1897. down or if not it is the equal.
  1898.  
  1899. This means that the jump is only done if the CF flag is deactivated or if
  1900. the ZF flag is deactivated, that is that one of the two be equal to zero.
  1901.  
  1902.  JAE (JNB) INSTRUCTION
  1903.  
  1904. Purpose: Conditional jump.
  1905.  
  1906. Syntax:
  1907.  
  1908. JAE label
  1909.  
  1910. It jumps if it is  or it is the equal or if it is not down.
  1911.  
  1912. The jump is done if CF is deactivated.
  1913.  
  1914.                 Assembler Tutorial
  1915.                          1996 Edition 
  1916.                    University of Guadalajara 
  1917.              Information Sistems General Coordination.
  1918.               Culture and Entertainment Web 
  1919.  
  1920.  JB (JNAE) INSTRUCTION
  1921.  
  1922. Purpose: Conditional jump.
  1923.  
  1924. Syntax:
  1925.  
  1926. JB label
  1927.  
  1928. It jumps if it is down, if it is not , or if it is the equal.
  1929.  
  1930. The jump is done if CF is activated.
  1931.  
  1932.  JBE (JNA) INSTRUCTION
  1933.  
  1934. Purpose: Conditional jump.
  1935.  
  1936. Syntax:
  1937.  
  1938. JBE label
  1939.  
  1940. It jumps if it is down, the equal, or if it is not .
  1941.  
  1942. The jump is done if CF is activated or if ZF is activated, that any of them
  1943. be equal to 1.
  1944.  
  1945.  JE (JZ) INSTRUCTION
  1946.  
  1947. Purpose: Conditional jump.
  1948.  
  1949. Syntax:
  1950.  
  1951. JE label
  1952.  
  1953. It jumps if it is the equal or if it is zero.
  1954.  
  1955. The jump is done if ZF is activated.
  1956.  
  1957.  
  1958.                 Assembler Tutorial
  1959.                          1996 Edition 
  1960.                    University of Guadalajara 
  1961.              Information Sistems General Coordination.
  1962.               Culture and Entertainment Web 
  1963.  
  1964.  JNE (JNZ) INSTRUCTION
  1965.  
  1966. Purpose: Conditional jump.
  1967.  
  1968. Syntax:
  1969.  
  1970. JNE label
  1971.  
  1972. It jumps if it is not equal or zero.
  1973.  
  1974. The jump will be done if ZF is deactivated.
  1975.  
  1976.  JG (JNLE) INSTRUCTION
  1977.  
  1978. Purpose: Conditional jump, and the sign is taken into account.
  1979.  
  1980. Syntax:
  1981.  
  1982. JG label
  1983.  
  1984. It jumps if it is larger, if it is not larger or equal.
  1985.  
  1986. The jump occurs if ZF = 0 or if OF = SF.
  1987.  
  1988.  JGE (JNL) INSTRUCTION
  1989.  
  1990. Purpose: Conditional jump, and the sign is taken into account.
  1991.  
  1992. Syntax:
  1993.  
  1994. JGE label
  1995.  
  1996. It jumps if it is larger or less than, or equal to.
  1997.  
  1998. The jump is done if SF = OF
  1999.  
  2000.  
  2001.  
  2002.                 Assembler Tutorial
  2003.                          1996 Edition 
  2004.                    University of Guadalajara 
  2005.              Information Sistems General Coordination.
  2006.               Culture and Entertainment Web 
  2007.  
  2008.  JL (JNGE) INSTRUCTION
  2009.  
  2010. Purpose: Conditional jump, and the sign is taken into account.
  2011.  
  2012. Syntax:
  2013.  
  2014. JL label
  2015.  
  2016. It jumps if it is less than or if it is not larger than or equal to.
  2017.  
  2018. The jump is done if SF is different than OF.
  2019.  
  2020.  JLE (JNG) INSTRUCTION
  2021.  
  2022. Purpose: Conditional jump, and the sign is taken into account.
  2023.  
  2024. Syntax:
  2025.  
  2026. JLE label
  2027.  
  2028. It jumps if it is less than or equal to, or if it is not larger.
  2029.  
  2030. The jump is done if ZF = 1 or if SF is defferent than OF.
  2031.  
  2032.  JC INSTRUCTION
  2033.  
  2034. Purpose: Conditional jump, and the flags are taken into account.
  2035.  
  2036. Syntax:
  2037.  
  2038. JC label
  2039.  
  2040. It jumps if there is cartage.
  2041.  
  2042. The jump is done if CF = 1
  2043.  
  2044.  
  2045.  
  2046.                 Assembler Tutorial
  2047.                          1996 Edition 
  2048.                    University of Guadalajara 
  2049.              Information Sistems General Coordination.
  2050.               Culture and Entertainment Web 
  2051.  
  2052.  JNC INSTRUCTION
  2053.  
  2054. Purpose: Conditional jump, and the state of the flags is taken into
  2055. account.
  2056.  
  2057. Syntax:
  2058.  
  2059. JNC label
  2060.  
  2061. It jumps if there is no cartage.
  2062.  
  2063. The jump is done if CF = 0.
  2064.  
  2065.  JNO INSTRUCTION
  2066.  
  2067. Purpose: Conditional jump, and the state of the flags is taken into
  2068. account.
  2069.  
  2070. Syntax:
  2071.  
  2072. JNO label
  2073.  
  2074. It jumps if there is no overflow.
  2075.  
  2076. The jump is done if OF = 0.
  2077.  
  2078.  JNP (JPO) INSTRUCTION
  2079.  
  2080. Purpose: Conditional jump, and the state of the flags is taken into
  2081. account.
  2082.  
  2083. Syntax:
  2084.  
  2085. JNP label
  2086.  
  2087. It jumps if there is no parity or if the parity is uneven.
  2088.  
  2089. The jump is done if PF = 0.
  2090.                 Assembler Tutorial
  2091.                          1996 Edition 
  2092.                    University of Guadalajara 
  2093.              Information Sistems General Coordination.
  2094.               Culture and Entertainment Web 
  2095.  
  2096.  JNS INSTRUCTION
  2097.  
  2098. Purpose: Conditional jump, and the state of the flags is taken into
  2099. account.
  2100.  
  2101. Syntax:
  2102.  
  2103. JNP label
  2104.  
  2105. It jumps if the sign is deactivated.
  2106.  
  2107. The jump is done if SF = 0.
  2108.  
  2109.  
  2110.  
  2111. JO INSTRUCTION
  2112.  
  2113. Purpose: Conditional jump, and the state of the flags is taken into
  2114. account.
  2115.  
  2116. Syntax:
  2117.  
  2118. JO label
  2119.  
  2120. It jumps if there is overflow.
  2121.  
  2122. The jump is done if OF = 1.
  2123.  
  2124.  JP (JPE) INSTRUCTION
  2125.  
  2126. Purpose: Conditional jump, the state of the flags is taken into account.
  2127.  
  2128. Syntax:
  2129.  
  2130. JP label
  2131.  
  2132. It jumps if there is parity or if the parity is even.
  2133.  
  2134.                 Assembler Tutorial
  2135.                          1996 Edition 
  2136.                    University of Guadalajara 
  2137.              Information Sistems General Coordination.
  2138.               Culture and Entertainment Web 
  2139.  
  2140. The jump is done if PF = 1.
  2141.  
  2142.  JS INSTRUCTION
  2143.  
  2144. Purpose: Conditional jump, and the state of the flags is taken into
  2145. account.
  2146.  
  2147. Syntax:
  2148.  
  2149. JS label
  2150.  
  2151. It jumps if the sign is on.
  2152.  
  2153. The jump is done if SF = 1.
  2154.  
  2155. 4.7 Instructions for cycles:loop
  2156.  
  2157. They transfer the process flow, conditionally or unconditionally, to a
  2158. destiny, repeating this action until the counter is zero.
  2159.  
  2160.  LOOP
  2161.  LOOPE
  2162.  LOOPNE
  2163.  
  2164.  
  2165.  
  2166.  LOOP INSTRUCTION
  2167.  
  2168. Purpose: To generate a cycle in the program.
  2169.  
  2170. Syntax:
  2171.  
  2172. LOOP label
  2173.  
  2174. The loop instruction decreases CX on 1, and transfers the flow of the
  2175. program to the label given as operator if CX is different than 1.
  2176.  
  2177.  
  2178.                 Assembler Tutorial
  2179.                          1996 Edition 
  2180.                    University of Guadalajara 
  2181.              Information Sistems General Coordination.
  2182.               Culture and Entertainment Web 
  2183.  
  2184.  LOOPE INSTRUCTION
  2185.  
  2186. Purpose: To generate a cycle in the program considering the state of ZF.
  2187.  
  2188. Syntax:
  2189.  
  2190. LOOPE label
  2191.  
  2192. This instruction decreases CX by 1. If CX is different to zero and ZF is
  2193. equal to 1, then the flow of the program is transferred to the label
  2194. indicated as operator.
  2195.  
  2196.  LOOPNE INSTRUCTION
  2197.  
  2198. Purpose: To generate a cycle in the program, considering the state of ZF.
  2199.  
  2200. Syntax:
  2201.  
  2202. LOOPNE label
  2203.  
  2204. This instruction decreases one from CX and transfers the flow of the
  2205. program only if ZF is different to 0.
  2206.  
  2207. 4.8 Counting instructions
  2208.  
  2209. They are used to decrease or increase the content of the counters.
  2210.  
  2211.  DEC
  2212.  INC
  2213.  
  2214.  DEC INSTRUCTION
  2215.  
  2216. Purpose: To decrease the operator.
  2217.  
  2218. Syntax:
  2219.  
  2220. DEC destiny
  2221.                 
  2222.                 Assembler Tutorial
  2223.                          1996 Edition 
  2224.                    University of Guadalajara 
  2225.              Information Sistems General Coordination.
  2226.               Culture and Entertainment Web 
  2227.  
  2228. This operation subtracts 1 from the destiny operator and stores the new
  2229. value in the same operator. 
  2230.  
  2231.  
  2232.  INC INSTRUCTION
  2233.  
  2234. Purpose: To increase the operator.
  2235.  
  2236. Syntax:
  2237.  
  2238. INC destiny The instruction adds 1 to the destiny operator and keeps the
  2239. result in the same destiny operator.
  2240.  
  2241. 4.9 Comparison instructions
  2242.  
  2243. They are used to compare operators, and they affect the content of the
  2244. flags.
  2245.  
  2246.  CMP
  2247.  CMPS (CMPSB) (CMPSW)
  2248.  
  2249.  
  2250.  CMP INSTRUCTION
  2251.  
  2252. Purpose: To compare the operators.
  2253.  
  2254. Syntax:
  2255.  
  2256. CMP destiny, source
  2257.  
  2258. This instruction subtracts the source operator from the destiny operator
  2259. but without this one storing the result of the operation, and it only
  2260. affects the state of the flags.
  2261.  
  2262.  
  2263.  
  2264.                 
  2265.  
  2266.                 Assembler Tutorial
  2267.                          1996 Edition 
  2268.                    University of Guadalajara 
  2269.              Information Sistems General Coordination.
  2270.               Culture and Entertainment Web 
  2271.  
  2272.  CMPS (CMPSB) (CMPSW) INSTRUCTION
  2273.  
  2274. Purpose: To compare chains of a byte or a word.
  2275.  
  2276. Syntax:
  2277.  
  2278. CMP destiny, source
  2279.  
  2280. With this instruction the chain of source characters is subtracted from the
  2281. destiny chain.
  2282.  
  2283. DI is used as an index for the extra segment of the source chain, and SI as
  2284. an index of the destiny chain.
  2285.  
  2286. It only affects the content of the flags and DI as well as SI are
  2287. incremented.
  2288.  
  2289. 4.10 Flag instructions
  2290.  
  2291. They directly affect the content of the flags.
  2292.  
  2293.  CLC
  2294.  CLD
  2295.  CLI
  2296.  CMC
  2297.  STC
  2298.  STD
  2299.  STI
  2300.  
  2301.  
  2302.  
  2303.  CLC INSTRUCTION
  2304.  
  2305. Purpose: To clean the cartage flag.
  2306.  
  2307. Syntax:
  2308.  
  2309.  
  2310.                 Assembler Tutorial
  2311.                          1996 Edition 
  2312.                    University of Guadalajara 
  2313.              Information Sistems General Coordination.
  2314.               Culture and Entertainment Web 
  2315.  
  2316. CLC
  2317.  
  2318. This instruction turns off the bit corresponding to the cartage flag, or in
  2319. other words it puts it on zero.
  2320.  
  2321.  CLD INSTRUCTION
  2322.  
  2323. Purpose: To clean the address flag.
  2324.  
  2325. Syntax:
  2326.  
  2327. CLD
  2328.  
  2329. This instruction turns off the corresponding bit to the address flag.
  2330.  
  2331.  CLI INSTRUCTION
  2332.  
  2333. Purpose: To clean the interruption flag.
  2334.  
  2335. Syntax:
  2336.  
  2337. CLI
  2338.  
  2339. This instruction turns off the interruptions flag, disabling this way
  2340. those maskarable interruptions.
  2341.  
  2342. A maskarable interruptions is that one whose functions are deactivated when
  2343. IF=0.
  2344.  
  2345.  CMC INSTRUCTION
  2346.  
  2347. Purpose: To complement the cartage flag.
  2348.  
  2349.  
  2350.  
  2351.  
  2352.  
  2353.  
  2354.                 Assembler Tutorial
  2355.                          1996 Edition 
  2356.                    University of Guadalajara 
  2357.              Information Sistems General Coordination.
  2358.               Culture and Entertainment Web 
  2359.  
  2360. Syntax:
  2361.  
  2362. CMC
  2363.  
  2364. This instruction complements the state of the CF flag, if CF = 0 the
  2365. instructions equals it to 1, and if the instruction is 1 it equals it to 0.
  2366.  
  2367. We could say that it only "inverts" the value of the flag.
  2368.  
  2369.  STC INSTRUCTION
  2370.  
  2371. Purpose: To activate the cartage flag.
  2372.  
  2373. Syntax:
  2374.  
  2375. STC
  2376.  
  2377. This instruction puts the CF flag in 1.
  2378.  
  2379.  STD INSTRUCTION
  2380.  
  2381. Purpose: To activate the address flag.
  2382.  
  2383. Syntax:
  2384.  
  2385. STD
  2386.  
  2387. The STD instruction puts the DF flag in 1.
  2388.  
  2389.  STI INSTRUCTION
  2390.  
  2391. Purpose: To activate the interruption flag.
  2392.  
  2393.  
  2394.  
  2395.  
  2396.  
  2397.  
  2398.                 Assembler Tutorial
  2399.                          1996 Edition 
  2400.                    University of Guadalajara 
  2401.              Information Sistems General Coordination.
  2402.               Culture and Entertainment Web 
  2403.  
  2404. Syntax:
  2405.  
  2406. STI
  2407.  
  2408. The instruction activates the IF flag, and this enables the maskarable
  2409. external interruptions ( the ones which only function when IF = 1).
  2410.  
  2411.  
  2412. 5 Interruptions and file managing                              
  2413.  
  2414. Table of Contents
  2415.  
  2416.  5.1 Internal hardware interruptions
  2417.  5.2 External hardware interruptions
  2418.  5.3 Software interruptions
  2419.  5.4 Most Common interruptions
  2420.  
  2421.  
  2422. 5.1 Internal hardware interruptions
  2423.  
  2424. Internal interruptions are generated by certain events which come  during
  2425. the execution of a program.
  2426.  
  2427. This type of interruptions are managed on their totality by the hardware
  2428. and it is not possible to modify them.
  2429.  
  2430. A clear example of this type of interruptions is the one which actualizes
  2431. the counter of the computer internal clock, the hardware makes the call to
  2432. this interruption several times during a second in order to maintain the
  2433. time  to date.
  2434.  
  2435. Even though we cannot directly manage this interruption, since we cannot
  2436. control the time dating by means of software, it is possible to use its
  2437. effects on the computer to our benefit, for example to create a "virtual
  2438. clock" dated continuously thanks to the clock's internal counter. We only
  2439. have to write a program which reads the actual value of the counter and to
  2440. translates it into an understandable format for the user.
  2441.  
  2442.                 Assembler Tutorial
  2443.                          1996 Edition 
  2444.                    University of Guadalajara 
  2445.              Information Sistems General Coordination.
  2446.               Culture and Entertainment Web 
  2447.  
  2448.  5.2 External hardware interruptions
  2449.  
  2450. External interruptions are generated by peripheral devices, such as
  2451. keyboards, printers, communication cards, etc. They are also generated by
  2452. coprocessors. It is not possible to deactivate external interruptions.
  2453.  
  2454. These interruptions are not sent directly to the CPU, but rather they are
  2455. sent to an integrated circuit whose function is to exclusively handle this
  2456. type of interruptions. The circuit, called PIC8259A, is controlled by the
  2457. CPU using for this control a series of communication ways called paths.
  2458.  
  2459.  
  2460.  5.3 Software interruptions
  2461.  
  2462. Software interruptions can be directly activated by the assembler invoking
  2463. the number of the desired interruption with the INT instruction.
  2464.  
  2465. The use of interruptions helps us in the creation of programs, and by using
  2466. them our programs are shorter, it is easier to understand them and they
  2467. usually have a better performance mostly due to their smaller size.
  2468.  
  2469. This type of interruptions can be separated in two categories: the
  2470. operative system DOS interruptions and the BIOS interruptions.
  2471.  
  2472. The difference between the two is that the operative system interruptions
  2473. are easier to use but they are also slower since these interruptions make
  2474. use of the BIOS to achieve their goal, on the other hand the BIOS
  2475. interruptions are much faster but they have the disadvantage that since
  2476. they are part of the hardware, they are very specific and can vary
  2477. depending even on the brand of the maker of the circuit.
  2478.  
  2479. The election of the type of interruption to use will depend solely on the
  2480. characteristics you want to give your program: speed, using the BIOS ones,
  2481. or portability, using the ones from the DOS.
  2482.  
  2483.  
  2484.  
  2485.  
  2486.                 Assembler Tutorial
  2487.                          1996 Edition 
  2488.                    University of Guadalajara 
  2489.              Information Sistems General Coordination.
  2490.               Culture and Entertainment Web 
  2491.  
  2492.                           
  2493. 5.4 Most common interruptions
  2494.  
  2495. Table of Contents
  2496.  
  2497. 5.4.1  Int 21H (DOS interruption)
  2498. Multiple calls to DOS functions.
  2499.  
  2500.  5.4.2 Int 10H (BIOS interruption)
  2501. Video input/output.
  2502.  
  2503.  5.4.3 Int 16H (BIOS interruption)
  2504. Keyboard input/output.
  2505.  
  2506.  5.4.4 Int 17H (BIOS interruption)
  2507. Printer input/output.
  2508.  
  2509.  
  2510.  5.41 21H Interruption
  2511. Purpose: To call on diverse DOS functions.
  2512.  
  2513. Syntax:
  2514.  
  2515. Int 21H
  2516.  
  2517. Note: When we work in TASM program is necessary to specify that the value we
  2518. are using is hexadecimal.
  2519.  
  2520. This interruption has several functions, to access each one of them it is
  2521. necessary that the function number which is required at the moment of
  2522. calling the interruption is in the AH register.
  2523.  
  2524. Functions to display information to the video.
  2525.  
  2526.      02H Exhibits output
  2527.      09H Chain Impression (video)
  2528.      40H Writing in device/file
  2529.  
  2530.                 Assembler Tutorial
  2531.                          1996 Edition 
  2532.                    University of Guadalajara 
  2533.              Information Sistems General Coordination.
  2534.               Culture and Entertainment Web 
  2535.  
  2536. Functions to read information from the keyboard.
  2537.  
  2538.      01H Input from the keyboard
  2539.      0AH Input from the keyboard using buffer
  2540.      3FH Reading from device/file
  2541.  
  2542. Functions to work with files.
  2543.  
  2544. In this section only the specific task of each function is exposed, for a
  2545. reference about the concepts used, refer to unit 7, titled : "Introduction
  2546. to file handling".
  2547.  
  2548. FCB Method
  2549.  
  2550.      0FH Open file
  2551.      14H Sequential reading
  2552.      15H Sequential writing
  2553.      16H Create file
  2554.      21H Random reading
  2555.      22H Random writing
  2556.  
  2557. Handles
  2558.  
  2559.      3CH Create file
  2560.      3DH Open file
  2561.      3EH Close file driver
  2562.      3FH Reading from file/device
  2563.      40H Writing in file/device
  2564.      42H Move pointer of reading/writing in file
  2565.  
  2566.  02H FUNCTION
  2567.  
  2568. Use:
  2569.  
  2570. It displays one character to the screen.
  2571.  
  2572.  
  2573.  
  2574.                 Assembler Tutorial
  2575.                          1996 Edition 
  2576.                    University of Guadalajara 
  2577.              Information Sistems General Coordination.
  2578.               Culture and Entertainment Web 
  2579.  
  2580. Calling registers:
  2581.  
  2582. AH = 02H
  2583. DL = Value of the character to display.
  2584.  
  2585. Return registers:
  2586.  
  2587. None.
  2588.  
  2589. This function displays the character whose hexadecimal code corresponds to
  2590. the value stored in the DL register, and no register is modified by using
  2591. this command.
  2592.  
  2593. The use of the 40H function is recommended instead of this function.
  2594.  
  2595.  09H FUNCTION
  2596.  
  2597. Use:
  2598.  
  2599. It displays a chain of characters on the screen.
  2600.  
  2601. Call registers:
  2602.  
  2603. AH = 09H
  2604. DS:DX = Address of the beginning of a chain of characters.
  2605.  
  2606. Return registers:
  2607.  
  2608. None.
  2609.  
  2610. This function displays the characters, one by one, from the indicated
  2611. address in the DS:DX register until finding a $ character, which is
  2612. interpreted as the end of the chain.
  2613.  
  2614. It is recommended to use the 40H function instead of this one.
  2615.  
  2616.  
  2617.  
  2618.                 Assembler Tutorial
  2619.                          1996 Edition 
  2620.                    University of Guadalajara 
  2621.              Information Sistems General Coordination.
  2622.               Culture and Entertainment Web 
  2623.  
  2624.  40H FUNCTION
  2625.  
  2626. Use:
  2627.  
  2628. To write to a device or a file.
  2629.  
  2630. Call registers:
  2631.  
  2632. AH = 40H
  2633. BX = Path of communication
  2634. CX = Quantity of bytes to write
  2635. DS:DX = Address of the beginning of the data to write
  2636.  
  2637. Return registers:
  2638.  
  2639. CF = 0 if there was no mistake
  2640.  
  2641.         AX = Number of bytes written
  2642.  
  2643. CF = 1 if there was a mistake
  2644.  
  2645.         AX = Error code
  2646.  
  2647. The use of this function to display information on the screen is done by
  2648. giving the BX register the value of 1 which is the preassigned value to the
  2649. video by the operative system MS-DOS.
  2650.  
  2651.  01H FUNCTION
  2652.  
  2653. Use:
  2654.  
  2655. To read a keyboard character and to display it.
  2656.  
  2657. Call registers
  2658.  
  2659. AH = 01H
  2660.  
  2661.  
  2662.                 Assembler Tutorial
  2663.                          1996 Edition 
  2664.                    University of Guadalajara 
  2665.              Information Sistems General Coordination.
  2666.               Culture and Entertainment Web 
  2667.  
  2668. Return registers:
  2669.  
  2670. AL = Read character
  2671.  
  2672. It is very easy to read a character from the keyboard with this function,
  2673. the hexadecimal code of the read character is stored in the AL register. In
  2674. case it is an extended register the AL register will contain the value of 0
  2675. and it will be necessary to call on the function again to obtain the code
  2676. of that character.
  2677.  
  2678.  0AH FUNCTION
  2679.  
  2680. Use:
  2681.  
  2682. To read keyboard characters and store them on the buffer.
  2683.  
  2684. Call registers:
  2685.  
  2686. AH = 0AH
  2687. DS:DX = Area of storage address
  2688. BYTE 0 = Quantity of bytes in the area
  2689. BYTE 1 = Quantity of bytes read
  2690. from BYTE 2 till BYTE 0 + 2 = read characters
  2691.  
  2692. Return characters:
  2693.  
  2694. None.
  2695.  
  2696. The characters are read and stored in a predefined space on memory. The
  2697. structure of this space indicate that in the first byte are indicated how
  2698. many characters will be read. On the second byte the number of characters
  2699. already read are stored, and from the third byte on the read characters are
  2700. written.
  2701.  
  2702. When all the indicated characters have been stored the speaker sounds and
  2703. any additional character is ignored. To end the capture of the chain it is
  2704. necessary to hit [ENTER].
  2705.  
  2706.                 Assembler Tutorial
  2707.                          1996 Edition 
  2708.                    University of Guadalajara 
  2709.              Information Sistems General Coordination.
  2710.               Culture and Entertainment Web 
  2711.  
  2712.  3FH FUNCTION
  2713.  
  2714. Use:
  2715.  
  2716. To read information from a device or file.
  2717.  
  2718. Call registers:
  2719.  
  2720. AH = 3FH
  2721. BX = Number assigned to the device
  2722. CX = Number of bytes to process
  2723. DS:DX = Address of the storage area
  2724.  
  2725. Return registers:
  2726.  
  2727. CF = 0 if there is no error and AX = number of read bytes.
  2728. CF = 1 if there is an error and AX will contain the error code.
  2729.  
  2730.  0FH FUNCTION
  2731.  
  2732. Use:
  2733.  
  2734. To open an FCB file
  2735.  
  2736. Call registers:
  2737.  
  2738. AH = 0FH
  2739. DS:DX = Pointer to an FCB
  2740.  
  2741. Return registers:
  2742.  
  2743. AL = 00H if there was no problem, otherwise it returns to 0FFH
  2744.  
  2745.  14H FUNCTION
  2746.  
  2747. Use:
  2748.  
  2749.  
  2750.                 Assembler Tutorial
  2751.                          1996 Edition 
  2752.                    University of Guadalajara 
  2753.              Information Sistems General Coordination.
  2754.               Culture and Entertainment Web 
  2755.  
  2756.  
  2757. To sequentially read an FCB file.
  2758.  
  2759. Call registers:
  2760.  
  2761. AH = 14H
  2762. DS:DX = Pointer to an FCB already opened.
  2763.  
  2764. Return registers:
  2765.  
  2766. AL = 0 if there were no errors, otherwise the corresponding error code will be returned:  1 error at the end of the file, 2 error on the FCB structure and 3 pa
  2767.  
  2768. What this function does is that it reads the next block of information from
  2769. the address given by DS:DX, and dates this register.
  2770.  
  2771.  15H FUNCTION
  2772.  
  2773. Use:
  2774.  
  2775. To sequentially write and FCB file.
  2776.  
  2777. Call registers:
  2778.  
  2779. AH = 15H
  2780. DS:DX = Pointer to an FCB already opened.
  2781.  
  2782. Return registers:
  2783.  
  2784. AL = 00H if there were no errors, otherwise it will contain the error code:  1 full disk or read-only file, 2 error on the formation or on the specification of
  2785.  
  2786. The 15H function dates the FCB after writing the register to the present
  2787. block.
  2788.  
  2789.  
  2790.  
  2791.  
  2792.                 Assembler Tutorial
  2793.                          1996 Edition 
  2794.                    University of Guadalajara 
  2795.              Information Sistems General Coordination.
  2796.               Culture and Entertainment Web 
  2797.  
  2798.  
  2799.  16H FUNCTION
  2800.  
  2801. Use:
  2802.  
  2803. To create an FCB file. Call registers:
  2804.  
  2805. AH = 16H
  2806. DS:DX = Pointer to an already opened FCB.
  2807.  
  2808. Return registers:
  2809.  
  2810. AL = 00H if there were no errors, otherwise it will contain the 0FFH value.
  2811.  
  2812. It is based on the information which comes on an FCB to create a file on a
  2813. disk.
  2814.  
  2815.  21H FUNCTION
  2816.  
  2817. Use:
  2818.  
  2819. To read in an random manner an FCB file.
  2820.  
  2821. Call registers:
  2822.  
  2823. AH = 21H
  2824. DS:DX = Pointer to and opened FCB.
  2825.  
  2826. Return registers:
  2827.  
  2828. A = 00H if there was no error, otherwise AH will contain the code of the error:  1 if it is the end of file, 2 if there is an FCB specification error and 3 if
  2829.  
  2830. This function reads the specified register by the fields of the actual block
  2831. and register of an opened FCB and places the information on the DTA, Disk
  2832. Transfer Area.
  2833.  
  2834.  
  2835.                 Assembler Tutorial
  2836.                          1996 Edition 
  2837.                    University of Guadalajara 
  2838.              Information Sistems General Coordination.
  2839.               Culture and Entertainment Web 
  2840.  
  2841.  
  2842.  22H FUNCTION
  2843.  
  2844. Use:
  2845.  
  2846. To write in an random manner an FCB file.
  2847.  
  2848. Call registers:
  2849.  
  2850. AH = 22H
  2851. DS:DX = Pointer to an opened FCB.
  2852.  
  2853. Return registers:
  2854.  
  2855. AL = 00H if there was no error, otherwise it will contain the error code:  1 if the disk is full or the file is an only read and 2 if there is an error on the
  2856.  
  2857. It writes the register specified by the fields of the actual block and
  2858. register of an opened FCB. It writes this information from the content of
  2859. the DTA.
  2860.  
  2861.  3CH FUNCTION
  2862.  
  2863. Use:
  2864.  
  2865. To create a file if it does not exist or leave it on 0 length if it exists,
  2866. Handle.
  2867.  
  2868. Call registers:
  2869.  
  2870. AH = 3CH
  2871. CH = File attribute
  2872. DS:DX = Pointer to an ASCII specification.
  2873.  
  2874. Return registers:
  2875. CF = 0 and AX the assigned number to handle if there is no error, in case there is, CF will be 1 and AX will contain the error code:  3 path not found, 4 there
  2876.  
  2877.                 Assembler Tutorial
  2878.                          1996 Edition 
  2879.                    University of Guadalajara 
  2880.              Information Sistems General Coordination.
  2881.               Culture and Entertainment Web 
  2882.  
  2883. This function substitutes the 16H function. The name of the file is
  2884. specified on an ASCII chain, which has as a characteristic being a
  2885. conventional chain of bytes ended with a 0 character.
  2886.  
  2887. The file created will contain the attributes defined on the CX register in
  2888. the following manner:
  2889.  
  2890.         Value           Attributes
  2891.          00H              Normal
  2892.          02H              Hidden
  2893.          04H              System
  2894.          06H              Hidden and of system
  2895.  
  2896. The file is created with the reading and writing permissions. It is not
  2897. possible to create directories using this function.
  2898.  
  2899.  3DH FUNCTION
  2900.  
  2901. Use:
  2902.  
  2903. It opens a file and returns a handle.
  2904.  
  2905. Call registers:
  2906.  
  2907. AH = 3DH
  2908. AL = manner of access
  2909. DS:DX = Pointer to an ASCII specification
  2910.  
  2911. Return registers:
  2912.  
  2913. CF = 0 and AX = handle number if there are no errors, otherwise CF = 1 and AX = error code: 01H if the function is not valid, 02H if the file was not found, 03
  2914.  
  2915. The returned handled is 16 bits.
  2916.  
  2917. The access code is specified in the following way:
  2918.  
  2919.  
  2920.                 Assembler Tutorial
  2921.                          1996 Edition 
  2922.                    University of Guadalajara 
  2923.              Information Sistems General Coordination.
  2924.               Culture and Entertainment Web 
  2925.  
  2926.  
  2927.         BITS
  2928.        7  6  5  4  3  2  1
  2929.  
  2930.        .  .  .  .  0  0  0      Only reading
  2931.        .  .  .  .  0  0  1      Only writing
  2932.        .  .  .  .  0  1  0      Reading/Writing
  2933.        .  .  .  x  .  .  .      RESERVED
  2934.  
  2935.  3EH FUNCTION
  2936.  
  2937. Use:
  2938.  
  2939. Close file (handle).
  2940.  
  2941. Call registers:
  2942.  
  2943. AH = 3EH
  2944. BX = Assigned handle
  2945.  
  2946. Return registers:
  2947.  
  2948. CF = 0 if there were no mistakes, otherwise CF will be 1 and AX will contain the error code:  06H if the handle is invalid.
  2949.  
  2950. This function dates the file and frees the handle it was using.
  2951.  
  2952.  3FH FUNCTION
  2953.  
  2954. Use:
  2955.  
  2956. To read a specific quantity of bytes from an open file and store them on a
  2957. specific buffer.
  2958.  
  2959.  
  2960.  
  2961.  
  2962.  
  2963.                 Assembler Tutorial
  2964.                          1996 Edition 
  2965.                    University of Guadalajara 
  2966.              Information Sistems General Coordination.
  2967.               Culture and Entertainment Web 
  2968.  
  2969.  
  2970. 5.4.2 10h Interruption
  2971.  
  2972. Purpose: To call on diverse BIOS video function
  2973.  
  2974. Syntax:
  2975.  
  2976. Int 10H
  2977.  
  2978. This interruption has several functions, all of them control the video
  2979. input/output, to access each one of them it is necessary that the function
  2980. number which is required at the moment of calling the interruption is in
  2981. the Ah register.
  2982.  
  2983. In this tutorial we will see some functions of the 10h interruption.
  2984.  
  2985. Common functions of the 10h interruption
  2986.  
  2987. 02H Function, select the cursor position
  2988. 09H Function, write attribute and character of the cursor
  2989. 0AH Function, write a character in the cursor position
  2990. 0EH Function, Alphanumeric model of the writing characters
  2991.  
  2992. 02h Function
  2993.  
  2994. Use:
  2995.  
  2996. Moves the cursor on the computer screen using text model.
  2997.  
  2998. Call registers:
  2999.  
  3000. AH = 02H
  3001.  
  3002. BH = Video page where the cursor is positioned.
  3003.  
  3004. DH = row
  3005.  
  3006.  
  3007.                 Assembler Tutorial
  3008.                          1996 Edition 
  3009.                    University of Guadalajara 
  3010.              Information Sistems General Coordination.
  3011.               Culture and Entertainment Web 
  3012.  
  3013.  
  3014. DL = Column
  3015.  
  3016. Return Registers:
  3017.  
  3018. None.
  3019.  
  3020. The cursor position is defined by its coordinates, starting from the
  3021. position 0,0 to position 79,24. This means from the left per computer
  3022. screen corner to right lower computer screen. Therefore the numeric values
  3023. that the DH and DL registers get in text model are: from 0 to 24 for rows
  3024. and from 0 to 79 for columns.
  3025.  
  3026. 09h Function
  3027.  
  3028. Use:
  3029.  
  3030. Shows a defined character several times on the computer screen with a
  3031. defined attribute, starting with the actual cursor position.
  3032.  
  3033. Call registers:
  3034.  
  3035. AH = 09H
  3036. AL = Character to display
  3037. BH = Video page, where the character will display it;
  3038. BL = Attribute to use
  3039. number of repetition.
  3040.  
  3041. Return registers:
  3042.  
  3043. None
  3044.  
  3045. This function displays a character on the computer screen several times,
  3046. using a specified number in the CX register but without changing the cursor
  3047. position on the computer screen.
  3048.  
  3049. 0Ah Function
  3050.  
  3051.                 Assembler Tutorial
  3052.                          1996 Edition 
  3053.                    University of Guadalajara 
  3054.              Information Sistems General Coordination.
  3055.               Culture and Entertainment Web 
  3056.  
  3057.  
  3058. Use:
  3059.  
  3060. Displays a character in the actual cursor position.
  3061.  
  3062. Call registers:
  3063.  
  3064. AH = 0AH
  3065. AL = Character to display
  3066. BH = Video page where the character will display it
  3067. BL = Color to use (graphic mode only).
  3068. CX = number of repetitions
  3069.  
  3070. Return registers:
  3071.  
  3072. None.
  3073.  
  3074. The main difference between this function and the last one is that this one
  3075. doesn't allow modifications on the attributes neither does it change the
  3076. cursor position.
  3077.  
  3078. 0EH Function
  3079.  
  3080. Use:
  3081.  
  3082. Displays a character on the computer screen dates the cursor position.
  3083.  
  3084. Call registers:
  3085.  
  3086. AH = 0EH
  3087. AL = Character to display
  3088. BH = Video page where the character will display it
  3089. BL = Color to use (graphic mode only).
  3090.  
  3091. Return registers:
  3092.  
  3093. None
  3094.  
  3095.                 Assembler Tutorial
  3096.                          1996 Edition 
  3097.                    University of Guadalajara 
  3098.              Information Sistems General Coordination.
  3099.               Culture and Entertainment Web 
  3100.  
  3101.  
  3102.  5.4.3 16H interruption
  3103.  
  3104. We will see two functions of the 16 h interruption, these functions are
  3105. called by using the AH register.
  3106.  
  3107. Functions of the 16h interruption
  3108.  
  3109. 00H Function, reads a character from the keyboard.
  3110. 01H Function, reads the keyboard state.
  3111.  
  3112. 00H Function Use:
  3113.  
  3114. Reads a character from the keyboard.
  3115.  
  3116. Call registers:
  3117.  
  3118. AH = 00H
  3119.  
  3120. Return registers:
  3121.  
  3122. AH = Scan code of the keyboard
  3123. AL = ASCII value of the character
  3124.  
  3125. When we use this interruption, the program executing is halted until a
  3126. character is typed, if this is an ASCII value; it is stored in the Ah
  3127. register, Else the scan code is stored in the AL register and the AH
  3128. register contents the value 00h.
  3129.  
  3130. The proposal of the scan code is to use it with the keys without ASCII
  3131. representation as [ALT][CONTROL], the function keys and so on.
  3132.  
  3133. 01h function
  3134.  
  3135. Use:
  3136.  
  3137. Reads the keyboard state
  3138.  
  3139.                 Assembler Tutorial
  3140.                          1996 Edition 
  3141.                    University of Guadalajara 
  3142.              Information Sistems General Coordination.
  3143.               Culture and Entertainment Web 
  3144.  
  3145. Call registers:
  3146.  
  3147. AH = 01H
  3148.  
  3149. Return registers:
  3150.  
  3151. If the flag register is zero, this means, there is information on the
  3152. buffer memory, else, there is no information in the buffer memory.
  3153. Therefore the value of the Ah register will be the value key stored in the
  3154. buffer memory.
  3155.  
  3156.  
  3157. 5.4.4 17H Interruption
  3158.  
  3159. Purpose: Handles the printer input/output.
  3160.  
  3161. Syntax:
  3162.  
  3163. Int 17H
  3164.  
  3165. This interruption is used to write characters on the printer, sets printer
  3166.  and reads the printer state.
  3167.  
  3168.      Functions of the 16h interruptions
  3169.  
  3170.           00H Function, prints value ASCII out
  3171.           01H Function, sets printer 
  3172.           02H Function, the printer state
  3173.  
  3174. 00H Function
  3175.  
  3176. Use:
  3177.  
  3178. Writes a character on the printer.
  3179.  
  3180.  
  3181.  
  3182.  
  3183.                 Assembler Tutorial
  3184.                          1996 Edition 
  3185.                    University of Guadalajara 
  3186.              Information Sistems General Coordination.
  3187.               Culture and Entertainment Web 
  3188. Call registers:
  3189.  
  3190.      AH = 00H
  3191.      AL = Character to print.
  3192.      DX = Port to use.
  3193.  
  3194. Return registers:
  3195.  
  3196.      AH = Printer device state.
  3197.  
  3198. The port to use is in the DX register, the different values are: LPT1 = 0,
  3199. LPT2 = 1, LPT3 = 2 ...
  3200.  
  3201. The printer device state is coded bit by bit as follows:
  3202.  
  3203.      BIT 1/0 MEANING
  3204.      ----------------------------------------
  3205.      0 1 The waited time is over
  3206.      1 -
  3207.      2 -
  3208.      3 1 input/output error
  3209.      4 1 Chosen printer
  3210.      5 1 out-of-paper
  3211.      6 1 communication recognized
  3212.      7 1 The printer is ready to use
  3213.  
  3214. 1 and 2 bits are not relevant
  3215.  
  3216. Most BIOS sport 3 parallel ports, although there are BIOS which sport 4
  3217. parallel ports.
  3218.  
  3219. 01h Function
  3220.  
  3221. Use:
  3222.  
  3223. Sets  parallel port.
  3224.  
  3225.  
  3226.  
  3227.                 Assembler Tutorial
  3228.                          1996 Edition 
  3229.                    University of Guadalajara 
  3230.              Information Sistems General Coordination.
  3231.               Culture and Entertainment Web 
  3232. Call registers:
  3233.  
  3234.      AH = 01H
  3235.      DX = Port to use
  3236.  
  3237. Return registers:
  3238.  
  3239.      AH = Printer status 
  3240.  
  3241. Port to use is defined in the DX register, for example: LPT=0, LPT2=1, and
  3242. so on.
  3243.  
  3244. The state of the printer is coded bit by bit as follows:
  3245.  
  3246.      BIT 1/0 MEANING
  3247.      ----------------------------------------
  3248.      0 1 The waited time is over
  3249.      1 -
  3250.      2 -
  3251.      3 1 input/output error
  3252.      4 1 Chosen printer
  3253.      5 1 out-of-paper
  3254.      6 1 communication recognized
  3255.      7 1 The printer is ready to use
  3256.  
  3257. 1 and 2 bits are not relevant
  3258.  
  3259. Most BIOS sport 3 parallel ports, although there are BIOS which sport 4
  3260. parallel ports.
  3261.  
  3262. 02h Function
  3263.  
  3264. Uses:
  3265.  
  3266. Gets the printer status.
  3267.  
  3268.  
  3269.  
  3270.  
  3271.                 Assembler Tutorial
  3272.                          1996 Edition 
  3273.                    University of Guadalajara 
  3274.              Information Sistems General Coordination.
  3275.               Culture and Entertainment Web 
  3276.  
  3277.  
  3278. Call registers:
  3279.  
  3280.      AH = 01H
  3281.      DX = Port to use
  3282.  
  3283. Return registers
  3284.  
  3285.      AH = Printer status.
  3286.  
  3287. Port to use is defined in the DX register, for example: LPT=0, LPT2=1, and
  3288. so on
  3289.  
  3290. The state of the printer is coded bit by bit as follows:
  3291.  
  3292.      BIT 1/0 MEANING
  3293.      ----------------------------------------
  3294.      0 1 The waited time is over
  3295.      1 -
  3296.      2 -
  3297.      3 1 input/output error
  3298.      4 1 Chosen printer
  3299.      5 1 out-of-paper
  3300.      6 1 communication recognized
  3301.      7 1 The printer is ready to use
  3302.  
  3303. 1 and 2 bits are not relevant
  3304.  
  3305. Most BIOS sport 3 parallel ports, although there are BIOS which sport 4
  3306. parallel ports. 
  3307.  
  3308.                            
  3309.  5.5 Ways of working with files
  3310.  
  3311. There are two ways to work with files, the first one is by means of file
  3312. control blocks or "FCB" and the second one is by means of communication
  3313. channels, also known as "handles".
  3314.  
  3315.                 Assembler Tutorial
  3316.                          1996 Edition 
  3317.                    University of Guadalajara 
  3318.              Information Sistems General Coordination.
  3319.               Culture and Entertainment Web 
  3320.  
  3321.  
  3322. The first way of file handling has been used since the CPM operative
  3323. system, predecessor of DOS, thus it assures certain compatibility with very
  3324. old files from the CPM as well as from the 1.0 version of the DOS, besides
  3325. this method allows us to have an unlimited number of open files at the same
  3326. time. If you want to create a volume for the disk the only way to achieve
  3327. this is by using this method.
  3328.  
  3329. Even after considering the advantages of the FCB, the use of the
  3330. communication channels it is much simpler and it allows us a better
  3331. handling of errors, besides, since it is much newer it is very probable
  3332. that the files created this way maintain themselves compatible through
  3333. later versions of the operative system.
  3334.  
  3335. For a greater facility on later explanations I will refer to the file
  3336. control blocks as FCBs and to the communication channels as handles.
  3337.  
  3338.  
  3339. 5.6 FCB method
  3340.  
  3341. 5.6.1 Introduction
  3342.  5.6.2 Open files
  3343.  5.6.3 Create a new file
  3344.  5.6.4 Sequential writing
  3345.  5.6.5 Sequential reading
  3346.  5.6.6Random reading and writing
  3347.  5.6.7 Close a file
  3348.  
  3349.  
  3350. 5.6.1 Introduction
  3351.  
  3352. There are two types of FCB, the normal, whose length is 37 bytes and the
  3353. extended one of 44 bytes.
  3354. On this tutorial we will only deal with the first type, so from now on when
  3355. I refer to an FCB, I am really talking about a 37 bytes FCB.
  3356.  
  3357. The FCB is composed of information given by the programmer and by
  3358. information which it takes directly from the operative system.
  3359.                 Assembler Tutorial
  3360.                          1996 Edition 
  3361.                    University of Guadalajara 
  3362.              Information Sistems General Coordination.
  3363.               Culture and Entertainment Web
  3364.  
  3365.  When thesetypes of files are used it is only possible to work on the current
  3366. directory since the FCBs do not provide sport for the use of the organization by directories of DOS.
  3367.  
  3368. The FCB is formed by the following fields:
  3369.  
  3370.    POSITION             LENGTH          MEANING
  3371.      00H                 1 Byte        Drive
  3372.      01H                 8 Bytes       File name
  3373.      09H                 3 Bytes       Extension
  3374.      0CH                 2 Bytes       Block number
  3375.      0EH                 2 Bytes       Register size
  3376.      10H                 4 Bytes       File size
  3377.      14H                 2 Bytes       Creation date
  3378.      16H                 2 Bytes       Creation hour
  3379.      18H                 8 Bytes       Reserved
  3380.      20H                 1 Bytes       Current register
  3381.      21H                 4 Bytes       Random register
  3382.  
  3383. To select the work drive the next format is followed: drive A = 1; drive B
  3384. = 2; etc. If 0 is used the drive being used at that moment will be taken as
  3385. option.
  3386.  
  3387. The name of the file must be justified to the left and in case it is
  3388. necessary the remaining bytes will have to be filled with spaces, and the
  3389. extension of the file is placed the same way.
  3390.  
  3391. The current block and the current register tell the computer which register
  3392. will be accessed on reading or writing operations. A block is a gro of
  3393. 128 registers. The first block of the file is the block 0. The first
  3394. register is the register 0, therefore the last register of the first block
  3395. would be the 127, since the numbering started with 0 and the block can
  3396. contain 128 registers in total.
  3397.  
  3398.  5.6.2 Opening files
  3399.  
  3400. To open an FCB file the 21H interruption, 0FH function is used. The unit,
  3401. the name and extension of the file must be initialized before opening it.
  3402.                 Assembler Tutorial
  3403.                          1996 Edition 
  3404.                    University of Guadalajara 
  3405.              Information Sistems General Coordination.
  3406.               Culture and Entertainment Web 
  3407.  
  3408.  
  3409. The DX register must point to the block. If the value of FFH is returned on
  3410. the AH register when calling on the interruption then the file was not
  3411. found, if everything came out well a value of 0 will be returned.
  3412.  
  3413. If the file is opened then DOS initializes the current block to 0, the size
  3414. of the register to 128 bytes and the size of the same and its date are
  3415. filled with the information found in the directory.
  3416.  
  3417. 5.6.3 Creating a new file
  3418.  
  3419. For the creation of files the 21H interruption 16H function is used.
  3420. DX must point to a control structure whose requirements are that at least
  3421. the logic unit, the name and the extension of the file be defined.
  3422. In case there is a problem the FFH value will be returned on AL, otherwise
  3423. this register will contain a value of 0.
  3424.  
  3425. 5.6.4 Sequential writing
  3426.  
  3427. Before we can perform writing to the disk it is necessary to define the
  3428. data transfer area using for this end the 1AH function of the 21H
  3429. interruption.
  3430.  
  3431. The 1AH function does not return any state of the disk nor or the
  3432. operation, but the 15H function, which is the one we will use to write to
  3433. the disk, does it on the AL register, if this one is equal to zero there
  3434. was no error and the fields of the current register and block are dated.
  3435.  
  3436. 5.6.5 Sequential reading
  3437.  
  3438. Before anything we must define the file transfer area or DTA.
  3439. In order to sequentially read we use the 14H function of the 21H
  3440. interruption.
  3441. The register to be read is the one which is defined by the current block
  3442. and register. The AL register returns to the state of the operation, if AL
  3443. contains a value of 1 or 3 it means we have reached the end of the file. A
  3444. value of 2 means that the FCB is wrongly structured.
  3445.  
  3446.                 Assembler Tutorial
  3447.                          1996 Edition 
  3448.                    University of Guadalajara 
  3449.              Information Sistems General Coordination.
  3450.               Culture and Entertainment Web 
  3451.  
  3452.  
  3453. In case there is no error, AL will contain the value of 0 and the fields of
  3454. the current block and register are dated.
  3455.  
  3456. 5.6.6 Random reading and writing
  3457.  
  3458. The 21H function and the 22H function of the 21H interruption are the ones
  3459. in charge of realizing the random readings and writings respectively.
  3460.  
  3461. The random register number and the current block are used to calculate
  3462. the relative position of the register to read or write.
  3463.  
  3464. The AL register returns the same information for the sequential reading of
  3465. writing. The information to be read will be returned on the transfer area
  3466. of the disk, likewise the information to be written resides on the DTA.
  3467.  
  3468.  5.6.7 Closing a file
  3469.  
  3470. To close a file we use the 10H function of the 21H interruption.
  3471.  
  3472. If after invoking this function, the AL register contains the FFH value,
  3473. this means that the file has changed position, the disk was changed or
  3474. there is error of disk access.
  3475.  
  3476. 5.7 Channels of communication
  3477.  
  3478. Table of Contents
  3479.  
  3480. 5.7.1  Working with handles
  3481.  5.7.2 Functions to use handles
  3482.  
  3483.  
  3484. 5.7.1 Working with handles
  3485.  
  3486. The use of handles to manage files greatly facilitates the creation of
  3487. files and programmer can concentrate on other aspects of the programming
  3488. without worrying on details which can be handled by the operative system.
  3489.  
  3490.                 Assembler Tutorial
  3491.                          1996 Edition 
  3492.                    University of Guadalajara 
  3493.              Information Sistems General Coordination.
  3494.               Culture and Entertainment Web 
  3495.  
  3496.  
  3497. The easy use of the handles consists in that to operate o a file, it is
  3498. only necessary to define the name of the same and the number of the handle
  3499. to use, all the rest of the information is internally handled by the DOS.
  3500.  
  3501. When we use this method to work with files, there is no distinction between
  3502. sequential or random accesses, the file is simply taken as a chain of
  3503. bytes.
  3504.  
  3505. 5.7.2 Functions to use handles
  3506.  
  3507. The functions used for the handling of files through handles are described
  3508. in unit 6: Interruptions, in the section dedicated to the 21H interruption.
  3509.  
  3510. 6 Macros and procedures                               
  3511.  
  3512. table of Contents
  3513.  
  3514.  6.1 Procedures
  3515. 6.2  Macros
  3516.  
  3517.  
  3518.  6.1 Procedure
  3519.  
  3520.  Definition of procedure
  3521.  
  3522. A procedure is a collection of instructions to which we can direct the flow
  3523. of our program, and once the execution of these instructions is over
  3524. control is given back to the next line to process of the code which called
  3525. on the procedure.
  3526.  
  3527. Procedures help us to create legible and easy to modify programs.
  3528.  
  3529. At the time of invoking a procedure the address of the next instruction of
  3530. the program is kept on the stack so that, once the flow of the program has
  3531. been transferred and the procedure is done, one can return to the next line
  3532. of the original program, the one which called the procedure.
  3533.  
  3534.                 Assembler Tutorial
  3535.                          1996 Edition 
  3536.                    University of Guadalajara 
  3537.              Information Sistems General Coordination.
  3538.               Culture and Entertainment Web 
  3539.  
  3540.  
  3541. Syntax of a Procedure
  3542.  
  3543. There are two types of procedures, the intrasegments, which are found on
  3544. the same segment of instructions, and the inter-segments which can be
  3545. stored on different memory segments.
  3546.  
  3547. When the intrasegment procedures are used, the value of IP is stored on the
  3548. stack and when the intrasegments are used the value of CS:IP is stored.
  3549.  
  3550. To divert the flow of a procedure (calling it), the following directive is
  3551. used:
  3552.  
  3553. CALL NameOfTheProcedure
  3554.  
  3555. The part which make  a procedure are:
  3556.  
  3557. Declaration of the procedure
  3558. Code of the procedure
  3559. Return directive
  3560. Termination of the procedure
  3561.  
  3562. For example, if we want a routine which adds two bytes stored in AH and AL
  3563. each one, and keep the addition in the BX register:
  3564.  
  3565. Adding Proc Near  ; Declaration of the procedure
  3566. Mov Bx, 0         ; Content of the procedure
  3567. Mov B1, Ah
  3568. Mov Ah, 00
  3569. Add Bx, Ax
  3570. Ret               ; Return directive
  3571. Add Endp          ; End of procedure declaration
  3572.  
  3573. On the declaration the first word, Adding, corresponds to the name of out
  3574. procedure, Proc declares it as such and the word Near indicates to the MASM
  3575. that the procedure is intrasegment. 
  3576.  
  3577.  
  3578.                 Assembler Tutorial
  3579.                          1996 Edition 
  3580.                    University of Guadalajara 
  3581.              Information Sistems General Coordination.
  3582.               Culture and Entertainment Web
  3583.  
  3584.  
  3585. The Ret directive loads the IP address stored on the stack to return to the original program, lastly, the Add Endp directive indicates the end of the procedure.
  3586.  
  3587. To declare an inter segment procedure we substitute the word Near for the
  3588. word FAR.
  3589.  
  3590. The calling of this procedure is done the following way:
  3591.  
  3592. Call Adding
  3593.  
  3594. Macros offer a greater flexibility in programming compared to the
  3595. procedures, nonetheless, these last ones will still be used.
  3596.  
  3597. 6.2 Macros
  3598.  
  3599.  6.2.1 Definition of a macro
  3600.  6.2.2 Syntax of a macro
  3601.  6.2.3 Macro libraries
  3602.  
  3603.  
  3604. 6.2.1 Definition of the macro
  3605.  
  3606. A macro is a gro of repetitive instructions in a program which are
  3607. codified only once and can be used as many times as necessary.
  3608.  
  3609. The main difference between a macro and a procedure is that in the macro
  3610. the passage of parameters is possible and in the procedure it is not, this
  3611. is only applicable for the TASM - there are other programming languages
  3612. which do allow it. At the moment the macro is executed each parameter is
  3613. substituted by the name or value specified at the time of the call.
  3614.  
  3615. We can say then that a procedure is an extension of a determined program,
  3616. while the macro is a module with specific functions which can be used by
  3617. different programs.
  3618.  
  3619.  
  3620.  
  3621.                 Assembler Tutorial
  3622.                          1996 Edition 
  3623.                    University of Guadalajara 
  3624.              Information Sistems General Coordination.
  3625.               Culture and Entertainment Web 
  3626.  
  3627. Another difference between a macro and a procedure is the way of calling
  3628. each one, to call a procedure the use of a directive is required, on the
  3629. other hand the call of macros is done as if it were an assembler
  3630. instruction.
  3631.  
  3632.  6.2.2 Syntax of a Macro
  3633.  
  3634. The parts which make  a macro are:
  3635.  
  3636. Declaration of the macro
  3637. Code of the macro
  3638. Macro termination directive
  3639.  
  3640. The declaration of the macro is done the following way:
  3641.  
  3642. NameMacro MACRO [parameter1, parameter2...]
  3643.  
  3644. Even though we have the functionality of the parameters it is possible to
  3645. create a macro which does not need them.
  3646.  
  3647. The directive for the termination of the macro is: ENDM
  3648.  
  3649. An example of a macro, to place the cursor on a determined position on the
  3650. screen is:
  3651.  
  3652. Position  MACRO  Row,  Column
  3653.   PUSH AX
  3654.   PUSH BX
  3655.   PUSH DX
  3656.   MOV AH,  02H
  3657.   MOV DH, Row
  3658.   MOV DL, Column
  3659.   MOV BH, 0
  3660.   INT 10H
  3661.   POP DX
  3662.   POP BX
  3663.   POP AX
  3664. ENDM
  3665.                 Assembler Tutorial
  3666.                          1996 Edition 
  3667.                    University of Guadalajara 
  3668.              Information Sistems General Coordination.
  3669.               Culture and Entertainment Web 
  3670.  
  3671. To use a macro it is only necessary to call it by its name, as if it were
  3672. another assembler instruction, since directives are no longer necessary as
  3673. in the case of the procedures. Example:
  3674.  
  3675. Position 8, 6
  3676.  
  3677.  6.2.3 Macro Libraries
  3678.  
  3679. One of the facilities that the use of macros offers is the creation of
  3680. libraries, which are groups of macros which can be included in a program
  3681. from a different file.
  3682.  
  3683. The creation of these libraries is very simple, we only have to write a
  3684. file with all the macros which will be needed and save it as a text file.
  3685.  
  3686. To call these macros it is only necessary to use the following instruction
  3687. Include NameOfTheFile, on the part of our program where we would normally
  3688. write the macros, this is, at the beginning of our program, before the
  3689. declaration of the memory model.
  3690.  
  3691.  The macros file was saved with the name of MACROS.TXT, the
  3692. instruction Include would be used the following way:
  3693.  
  3694.           ;Beginning of the program
  3695.         Include MACROS.TXT
  3696.         .MODEL SMALL
  3697.         .DATA
  3698.           ;The data goes here
  3699.         .CODE
  3700.         Beginning:
  3701.           ;The code of the program is inserted here
  3702.         .STACK
  3703.           ;The stack is defined
  3704.         End beginning
  3705.           ;Our program ends
  3706.  
  3707.   
  3708.  
  3709.                 Assembler Tutorial
  3710.                          1996 Edition 
  3711.                    University of Guadalajara 
  3712.              Information Sistems General Coordination.
  3713.               Culture and Entertainment Web 
  3714.  
  3715.  
  3716. More debug program examples
  3717.  
  3718. In this section we provide you several assembler programs to run in the
  3719. debug program. You can execute each assembler program using the "t" (trace) command, to see what each instruction does.
  3720.  
  3721. First example
  3722.  
  3723. -a0100
  3724. 297D:0100     MOV     AX,0006        ; Puts value 0006 at register AX
  3725. 297D:0103     MOV     BX,0004        ;Puts value 0004 at register BX
  3726. 297D:0106     ADD     AX,BX        ;Adds BX to AX contents
  3727. 297D:0108      INT     20         ;Causes end of the Program
  3728.  
  3729. The only thing that this program does is to save two values in two
  3730. registers and add the value of one to the other.
  3731.  
  3732. Second example
  3733.  
  3734. - a100
  3735. 0C1B:0100 jmp 125 ; Jumps to direction 125H
  3736. 0C1B:0102 [Enter]
  3737. - e 102  'Hello, How are you ?' 0d 0a '$'
  3738. - a125
  3739. 0C1B:0125 MOV DX,0102 ; Copies string to DX register
  3740. 0C1B:0128 MOV CX,000F ; Times the string will be displayed
  3741. 0C1B:012B MOV AH,09 ; Copies 09 value to AH register
  3742. 0C1B:012D INT 21 ; Displays string
  3743. 0C1B:012F DEC CX ; Reduces in 1 CX
  3744. 0C1B:0130 JCXZ 0134 ; If CX is equal to 0 jumps to 0134
  3745. 0C1B:0132 JMP 012D ; Jumps to direction 012D
  3746. 0C1B:0134 INT 20 ; Ends the program
  3747.  
  3748. This program displays on the screen 15 times a character string.
  3749.  
  3750. Third example
  3751.  
  3752. -a100
  3753. 297D:0100       MOV     AH,01    ;Function to change the cursor
  3754. 297D:0102       MOV     CX,0007 ;Forms the cursor
  3755. 297D:0105       INT     10 ;Calls for BIOS
  3756. 297D:0107       INT     20 ;Ends the program
  3757.  
  3758. This program is good for changing the form of the cursor.
  3759.  
  3760. Fourth example
  3761.  
  3762. -a100
  3763. 297D:0100         MOV     AH,01 ; Funtion  1 (reads keyboard)
  3764. 297D:0102         INT     21 ; Calls for DOS
  3765. 297D:0104        CMP     AL,0D ; Compares if what is read is a carriage return
  3766. 297D:0106        JNZ     0100 ; If it is not, reads another character
  3767. 297D:0108        MOV     AH,02 ; Funtion 2 (writes on the screen)
  3768. 297D:010A       MOV     DL,AL ; Character to write on AL
  3769. 297D:010C       INT     21 ; Calls for DOS
  3770. 297D:010E       INT     20 ; Ends the program
  3771.  
  3772. This program uses DOS 21H interruption. It uses two functions of the same:
  3773. the first one reads the keyboard (function 1) and the second one writes on
  3774. the screen. It reads the keyboard characters until it finds a carriage
  3775. return.
  3776.  
  3777. Fifth example
  3778.  
  3779. -a100
  3780. 297D:0100       MOV     AH,02 ;  Function 2 (writes on the screen)
  3781. 297D:0102       MOV     CX,0008 ; Puts value 0008 on register CX
  3782. 297D:0105       MOV     DL,00 ;  Puts value 00 on register DL
  3783. 297D:0107      RCL     BL,1 ;   Rotates the byte in BL to the left by one bit through the                                          ;carry flag
  3784. 297D:0109      ADC     DL,30 ; Converts flag register to1
  3785. 297D:010C      INT     21 ; Calls for DOS
  3786. 297D:010E      LOOP    0105 ; Jumps if CX > 0  to direction 0105
  3787. 297D:0110       INT     20 ; Ends the program
  3788.  
  3789. This program displays on the screen a binary number through a conditional
  3790. cycle (LOOP) using byte rotation.
  3791.  
  3792. Sixth example
  3793.  
  3794. -a100
  3795. 297D:0100       MOV     AH,02 ; Function 2 (writes on the screen)
  3796. 297D:0102       MOV     DL,BL ; Puts BL's value on DL
  3797. 297D:0104       ADD     DL,30 ; Adds value 30 to DL
  3798. 297D:0107       CMP     DL,3A ; Compares 3A value with DL's contents without affecting                           ; its value only modifying the state of the car
  3799. 297D:010A      JL      010F ; jumps if < direction 010f
  3800. 297D:010C      ADD     DL,07 ;  Adds 07 value on DL
  3801. 297D:010F       INT     21 ; Calls for Dos
  3802. 297D:0111       INT     20 ; Ends the Program
  3803.  
  3804. This program prints a zero value on hex digits
  3805.  
  3806. Seventh example
  3807.  
  3808. -a100
  3809. 297D:0100      MOV     AH,02 ; Function 2 (writes on the screen)
  3810. 297D:0102      MOV     DL,BL ;  Puts BL value on DL
  3811. 297D:0104      AND     DL,0F ;  Carries ANDing numbers bit by bit
  3812. 297D:0107      ADD     DL,30 ; Adds 30 to Dl
  3813. 297D:010A     CMP     DL,3A ;  Compares Dl with 3A
  3814. 297D:010D      JL      0112 ; Jumps if < 0112 direction
  3815. 297D:010F      ADD     DL, 07 ; Adds  07 to DL
  3816. 297D:0112      INT     21 ; Calls for Dos
  3817. 297D:0114      INT     20 ;Ends the program
  3818.  
  3819. This program is used to print two digit hex numbers.
  3820.  
  3821. Eight example
  3822.  
  3823. -a100
  3824. 297D:0100       MOV     AH,02 ; Function 2 (writes on the screen)
  3825. 297D:0102       MOV     DL,BL ; Puts BL value on DL
  3826. 297D:0104       MOV     CL,04 ;  Puts 04 value on CL
  3827. 297D:0106       SHR     DL,CL ;  Moves per four bits of your number to the rightmost                                          ;nibble
  3828. 297D:0108       ADD     DL,30 ; Adds  30 to DL
  3829. 297D:010B      CMP     DL,3A ; Compares Dl with 3A
  3830. 297D:010E       JL      0113 ; Jumps if < 0113 direction
  3831. 297D:0110       ADD     DL,07 ; Adds 07 to DL
  3832. 297D:0113       INT     21 ; Calls for Dos
  3833. 297D:0115       INT     20 ; Ends the program
  3834.  
  3835. This program works for printing the first of two digit hex numbers
  3836.  
  3837. Ninth example
  3838.  
  3839. -a100
  3840. 297D:0100      MOV     AH,02 ; Function 2 (writes on the screen)
  3841. 297D:0102      MOV     DL,BL ; Puts BL value on DL
  3842. 297D:0104      MOV     CL,04 ; Puts 04 value on CL
  3843. 297D:0106      SHR     DL,CL ; Moves per four bits of your number to the rightmost                                            ;nibble
  3844. 297D:0108      ADD     DL,30 ; Adds 30 to DL
  3845. 297D:010B      CMP     DL,3A ; Compares Dl with 3A
  3846. 297D:010E      JL      0113 ; Jumps if < 0113 direction
  3847. 297D:0110      ADD     DL,07 ; Adds 07 to DL
  3848. 297D:0113      INT     21 ; Calls for Dos
  3849. 297D:0115      MOV     DL,BL ; Puts Bl value on DL
  3850. 297D:0117      AND     DL,0F ; Carries ANDing numbers bit by bit
  3851. 297D:011A      ADD     DL,30 ; Adds 30 to DL
  3852. 297D:011D      CMP     DL,3A ; Compares Dl with 3A
  3853. 297D:0120       JL      0125 ; Jumps if < 125 direction
  3854. 297D:0122      ADD     DL,07 ; Adds 07 to DL
  3855. 297D:0125      INT     21 ; Calls for Dos
  3856. 297D:0127      INT     20 ; Ends the Program
  3857.  
  3858. This program works for printing the second of two digit hex numbers
  3859.  
  3860. Tenth example
  3861.  
  3862. -a100
  3863. 297D:0100       MOV     AH,01 ; Function 1 (reads keyboard)
  3864. 297D:0102       INT     21 ; Calls for  Dos
  3865. 297D:0104       MOV     DL,AL ; Puts Al value on DL
  3866. 297D:0106       SUB     DL,30 ;  Subtracts 30 from DL
  3867. 297D:0109       CMP     DL,09 ; Compares DL with 09
  3868. 297D:010C       JLE     0111;  Jumps if <=  0111 direction
  3869. 297D:010E       SUB     DL,07 ; Subtracts 07 from DL
  3870. 297D:0111       MOV     CL,04 ; Puts 04 value on CL register
  3871. 297D:0113       SHL     DL,CL ; It inserts zeros to the right
  3872. 297D:0115       INT     21 ; Calls for Dos
  3873. 297D:0117       SUB     AL,30 ; Subtracts 30 from AL
  3874. 297D:0119       CMP     AL,09 ; Compares AL with 09
  3875. 297D:011B       JLE     011F ; Jumps if <= 011f direction
  3876. 297D:011D      SUB     AL,07 ; Subtracts 07 from AL
  3877. 297D:011F       ADD     DL,AL ; Adds Al to DL
  3878. 297D:0121        INT     20 ; Ends the Program
  3879.  
  3880. This program can read two digit hex numbers
  3881.  
  3882. Eleventh example
  3883.  
  3884. -a100
  3885. 297D:0100      CALL    0200 ;  Calls for a procedure
  3886. 297D:0103      INT     20 ;Ends the program
  3887.  
  3888. -a200
  3889. 297D:0200      PUSH    DX ;  Puts DX value on the stack
  3890. 297D:0201      MOV     AH,08 ; Function 8
  3891. 297D:0203      INT     21 ; Calls for Dos
  3892. 297D:0205      CMP     AL,30 ; Compares AL with 30
  3893. 297D:0207      JB      0203 ; Jumps if CF is activated towards 0203 direction
  3894. 297D:0209     CMP     AL,46 ; Compares AL with 46
  3895. 297D:020B     JA      0203 ; jumps if <> 0203 direction
  3896. 297D:020D    CMP     AL,39 ; Compares AL with 39
  3897. 297D:020F     JA      021B ; Jumps if <> 021B direction
  3898. 297D:0211     MOV     AH,02 ; Function 2 (writes on the screen)
  3899. 297D:0213     MOV     DL,AL ; Puts Al value on DL
  3900. 297D:0215     INT     21 ; Calls for Dos
  3901. 297D:0217     SUB     AL,30 ; Subtracts 30 from AL
  3902. 297D:0219     POP     DX ;  Takes DX value out of the stack
  3903. 297D:021A    RET ; Returns control to the main program
  3904. 297D:021B    CMP     AL,41 ; Compares AL with 41
  3905. 297D:021D    JB      0203 ; Jumps if CF is activated towards 0203 direction
  3906. 297D:021F     MOV     AH,02 ; Function 2 (writes on the screen)
  3907. 297D:022       MOV     DL,AL ; Puts AL value on DL
  3908. 297D:0223     INT     21 ; Calls for Dos
  3909. 297D:0225     SUB     AL,37 ; Subtracts 37 from AL
  3910. 297D:0227     POP     DX ; Takes DX value out of the stack
  3911. 297D:0228     RET ; Returns control to the main program
  3912.  
  3913. This program keeps reading characters until it receives one that can be
  3914. converted to a hex number
  3915.  
  3916.  
  3917.      More Assembler programs examples( using TASM program)                          
  3918.  
  3919. ;name of the program:one.asm
  3920. ;
  3921. .model small
  3922. .stack
  3923. .code
  3924.         mov AH,1h       ;Selects the 1  D.O.S. function
  3925.         Int 21h         ;reads character and return ASCII code to register AL
  3926.         mov DL,AL       ;moves the ASCII code to register DL
  3927.         sub DL,30h      ;makes the operation minus 30h to convert 0-9 digit number
  3928.         cmp DL,9h       ;compares if digit number it was between 0-9
  3929.         jle digit1      ;If it true gets the first number digit (4 bits long)
  3930.         sub DL,7h       ;If it false, makes operation minus 7h to convert letter A-F
  3931. digit1:
  3932.         mov CL,4h       ;prepares to multiply by 16
  3933.         shl DL,CL       ; multiplies to convert into four bits upper
  3934.         int 21h         ;gets the next character
  3935.         sub AL,30h      ;repeats the conversion operation
  3936.         cmp AL,9h       ;compares the value 9h with the content of register AL
  3937.         jle digit2      ;If true, gets the second digit number
  3938.         sub AL,7h       ;If no, makes the minus operation 7h
  3939. digit2:
  3940.         add DL,AL       ;adds the second number digit
  3941.         mov AH,4CH
  3942.         Int 21h         ;21h interruption
  3943.         End; finishs the program code
  3944.  
  3945. This program reads two characters from the keyboard and prints them on the screen.
  3946.  
  3947. ;name the program:two.asm
  3948. .model small
  3949. .stack
  3950. .code
  3951. PRINT_A_J       PROC
  3952.         MOV DL,'A'  ;moves the A character to register DL
  3953.         MOV CX,10   ;moves the decimal value 10 to register cx
  3954.                         ;This number value its the time to print out after the A                                        ;character
  3955. PRINT_LOOP:
  3956.         CALL WRITE_CHAR ;Prints A character out
  3957.         INC DL          ;Increases the value of register DL
  3958.         LOOP PRINT_LOOP ;Loop to print out ten characters
  3959.         MOV AH,4Ch      ;4Ch function of the 21h interruption
  3960.         INT 21h         ;21h interruption
  3961. PRINT_A_J       ENDP    ;Finishes the procedure
  3962.  
  3963. WRITE_CHAR      PROC
  3964.         MOV AH,2h   ;2h function of the 21 interruption
  3965.         INT 21h     ;Prints character out from the register DL
  3966.         RET         ;Returns the control to procedure called
  3967. WRITE_CHAR      ENDP     ;Finishes the procedure
  3968.         END  PRINT_A_J   ;Finishes the program code
  3969.  
  3970. This progrma prints the a character through j character on the screen
  3971.  
  3972. ;name of the program:three.asm
  3973. .model small
  3974. .STACK
  3975. .code
  3976.  
  3977. TEST_WRITE_HEX    PROC
  3978.         MOV DL,3Fh     ;moves the value 3Fh to the register DL
  3979.         CALL WRITE_HEX ;Calls the procedure
  3980.         MOV AH,4CH     ;4Ch function
  3981.         INT 21h        ;Returns the control to operating system
  3982. TEST_WRITE_HEX ENDP    ;Finishes the procedure
  3983.  
  3984.         PUBLIC WRITE_HEX
  3985. ;........................................................;
  3986. ; This procedure converts into hexadecimal number the byte is in the register DL and show the digit number;
  3987. ;Use:WRITE_HEX_DIGIT                                 ;
  3988. ;........................................................;
  3989.  
  3990. WRITE_HEX    PROC
  3991.         PUSH CX     ;pushes the value of the register CX to the stack memory
  3992.         PUSH DX     ;pushes the value of the register DX to the stack memory
  3993.       MOV DH,DL   ;moves the value of the register DL to register DH
  3994.         MOV CX,4    ;moves the value numeric 4 to register CX
  3995.         SHR DL,CL
  3996.         CALL WRITE_HEX_DIGIT ;shows on the computer screen, the first hexadecimal number
  3997.         MOV DL,DH   ;moves the value of the register DH to the register DL
  3998.         AND DL,0Fh  ;ANDing the upper bit
  3999.         CALL WRITE_HEX_DIGIT ; shows on the computer screen, the second hexadecimal number
  4000.         POP DX      ;pops the value of the register DX to register DX
  4001.         POP CX      ; pops the value of the register DX to register DX
  4002.         RET         ;Returns the control of the procedure called
  4003. WRITE_HEX  ENDP
  4004.  
  4005.         PUBLIC WRITE_HEX_DIGIT
  4006. ;......................................................................;
  4007. ;                                                                      ;
  4008. ; This procedure converts the lower 4 bits of the register DL into hexadecimal ;number and show them in the computer screen    ;
  4009. ;Use: WRITE_CHAR                                                   ;
  4010. ;......................................................................;
  4011.  
  4012. WRITE_HEX_DIGIT    PROC
  4013.         PUSH DX     ;Pushes the value of the register DX in the stack memory
  4014.         CMP DL,10   ;compares if the bit number is minus than number ten
  4015.         JAE HEX_LETTER  ;No , jumps HEX_LETER
  4016.         ADD DL,"0"  ;yes, it converts into digit number
  4017.         JMP Short WRITE_DIGIT ;writes the character
  4018. HEX_LETTER:
  4019.         ADD DL,"A"-10 ;converts a character into hexadecimal number
  4020. WRITE_DIGIT:
  4021.         CALL WRITE_CHAR ;shows the character in the computer screen
  4022.         POP DX      ;Returns the initial value of the register DX to register DL
  4023.         RET         ;Returns the control of the procedure called
  4024. WRITE_HEX_DIGIT   ENDP
  4025.  
  4026.         PUBLIC WRITE_CHAR
  4027. ;......................................................................;
  4028. ;This procedure shows the character in the computer screen using the D.O.S. ;
  4029. ;......................................................................;
  4030.  
  4031. WRITE_CHAR   PROC
  4032.         PUSH AX   ;pushes the value of the register AX in the stack memory
  4033.         MOV AH,2  ;2h Function
  4034.         INT 21h   ;21h Interruption
  4035.         POP AX    ;Pops the initial value of the register AX to the register AX
  4036.         RET       ;Returns the control of the procedure called
  4037. WRITE_CHAR   ENDP
  4038.  
  4039.         END TEST_WRITE_HEX ;finishes the program code
  4040.  
  4041. This program prints a predefined value on the screen
  4042.  
  4043.  
  4044. ;name of the program:five.asm
  4045. .model small
  4046. .stack
  4047. .code
  4048.  
  4049. PRINT_ASCII       PROC
  4050.         MOV DL,00h  ;moves the value 00h to register DL
  4051.         MOV CX,255   ;moves the value decimal number 255. this decimal number will be 255 times to print out after the character A
  4052. PRINT_LOOP:
  4053.         CALL WRITE_CHAR ;Prints the characters out
  4054.         INC DL          ;Increases the value of the register DL content
  4055.         LOOP PRINT_LOOP ;Loop to print out ten characters
  4056.         MOV AH,4Ch      ;4Ch function
  4057.         INT 21h         ;21h Interruption
  4058. PRINT_ASCII       ENDP    ;Finishes the procedure
  4059.  
  4060. WRITE_CHAR      PROC
  4061.         MOV AH,2h   ;2h function to print character out
  4062.         INT 21h     ;Prints out the character in the register DL
  4063.         RET         ;Returns the control to the procedure called
  4064. WRITE_CHAR      ENDP     ;Finishes the procedure
  4065.  
  4066.         END  PRINT_ASCII   ;Finishes the program code
  4067.  
  4068. This program prints the 256 ASCII code on the screen
  4069.  
  4070. dosseg
  4071. .model small
  4072. .stack
  4073. .code
  4074. write   proc        
  4075.         mov ah,2h; 
  4076.         mov dl,2ah; 
  4077.         int 21h
  4078.         mov ah,4ch
  4079.         int 21h
  4080. write  endp
  4081.  
  4082.         end  write
  4083.  
  4084. This program prints a defined character using an ASCII code on the screen.
  4085.  
  4086.  
  4087. .model small; the name of the program is seven.asm
  4088. .stack; 
  4089. .code; 
  4090.  
  4091. EEL:    MOV AH,01 ; 1 function (reads one character from the keyboard)
  4092.         INT 21h ; 21h interruption
  4093.         CMP AL,0Dh ; compares the value with  0dh
  4094.         JNZ EEL ;jumps if no equal of the label eel
  4095.         MOV AH,2h ; 2 function (prints the character out on the screen)
  4096.         MOV DL,AL ;moves the value of the register  AL to the register  DL
  4097.         INT 21h  ;21 interruption
  4098.         MOV AH,4CH ;4C function (returns the control to the D.O.S. operating system)
  4099.         INT 21h ;21 interruption
  4100.         
  4101.         END ;finishes the program
  4102.  
  4103. This program reads characters form the keyboard and prints them on the screen until find the return character.
  4104.  
  4105.  
  4106.